aks
aks

Reputation: 4825

Hi, i have a C programming question. I want to know the difference between the two and where one is useful over other?

I have a C programming question.

I want to know the difference between the two and where one is useful over other?

Suppose I have a struct called employee as below:

struct emp{
   char first_name[10];
   char last_name[10];
   char key[10];
};

Now, I want to store the table of employee records, then which method should I use:

  1. struct emp e1[100]; // Or
  2. struct emp * e1[100];

I know the two are not same but would like to know a use case where second declaration would be of interest and more advantageous to use.

Can someone clarify?

Upvotes: 1

Views: 284

Answers (4)

Sunscreen
Sunscreen

Reputation: 3564

You can use the first approact if you know the actual number of employees (rarely the case) at compile time. On the other hand, if you do not know the number of employees it is even easier not to specify the number of your pointers like that:

emp* myEl = NULL;

When you have the number of employees, lets say nMyEmployees, you can allocate the structures you want like that:

myEl = new emp[nMyNumber];

and initalise them

memset(&myEl, 0x00, sizeof(emp));

Now you are ready to use them by:

myEl[0].first_name;

Note that you need to free this memory when you are done:

if (myEl != NULL) delete [] myEl;

Upvotes: -1

Vikas
Vikas

Reputation: 1

First is array of struct emp and second is pointer to array of struct emp.

You don't need to allocate space in both the cases the difference is only the way of accessing the elements.

For first one if you want to store some value you will have to use - e1[0].key = 'k'; and read it back using

printf("%c", e1[0].key);

And for second one you should access access -> operator

e1[0]->key = 'k'; and read it back using

printf("%c", e1[0]->key);

The difference is 1] In first case you have the variable that has address of array e1[100]. 2] In second case you have the variable that point to the address of array of e1[100].

Upvotes: 0

XAder
XAder

Reputation: 676

Second preferably since you can have empty records meaning it's very easy to distinguish between empty or used record.

But I would go for some utility functions to manage records inside kind of list.

Don't know about pure c if there's a something similar to std::list, if there's something like that I would use it.

Upvotes: 0

Marcelo Cantos
Marcelo Cantos

Reputation: 186118

The second is an array of pointers to struct emp, meaning that you have to allocate each element on the heap. The advantage is that you only have to allocate enough memory for the number of emps you actually have, but the disadvantage is that you have to manage the lifetime of each emp.

In the first form, the 100 emps are stored in the array itself, so they do not require separate allocation. But this array takes up enough space for all 100 emps, even if you only have, say, ten emps.

Upvotes: 7

Related Questions