Question

I have 3 different variable-sized arrays. Simply one is 2D char array, and the other two are unsigned int arrays.

Let them;

char names[numberOfPeople][MAX_NAME_SIZE + 1];
unsigned int ages[numberOfPeople];
unsigned int weights[numberOfPeople];

Let the people have common names, so the macro MAX_NAME_SIZE is defined as 10 as preprocessor. numberOfPeople is asked to the user, user enters it with fgets and after some procedure it is read to the variable.

It's OK to have VLA's but the function which gets the numberOfPeople variable's value from the user comes after the array definitions. So arrays are defined with initialized value of numberOfPeople before it's changed.

Now, the problem is; how can I allocate memory dynamically and afterwards initialize the arrays? I've read some documentation about calloc , but as you can see, the arrays are not pointer to arrays. So I couldn't find the correct syntax for using calloc. It returns to void *, how can I achieve this?

Do I have to define arrays as pointer to arrays? And the first one is a char array, does it matters?

Was it helpful?

Solution

Perhaps the pseudo-code below will help you on your way.

Instead of defining arrays at compile time, define them dynamically at run-time. Since, at compile time, you won't know the size of the arrays, just define them to 'point' to arrays, and worry about assigning memory to the arrays after their size is known (at run-time):

char *names;
unsigned int *ages = NULL;
unsigned int *weights = NULL;

I assume (since you didn't share this information in your question) that your code has storage similar to the following, used with fgets() to request "the number of people" as a string:

char tmpStr[MAX_NAME_SIZE];
int numberOfPeople;

printf("Enter the number of people: *");
fgets(tempStr, sizeof(tmpStr), stdin);
numberOfPeople = atoi(tmpStr);

Now that you know the numberOfPeople, allocate an array of name pointers, an array of ages integers, and an array of weights integers.

names = malloc(numberOfPeople * sizeof(*names));
ages = malloc(numberOfPeople * sizeof(*ages));
weights=malloc(numberOfPeople * sizeof(*weights));

To initialize the arrays, I (again) assume your code does something like a for loop, where you ask the user for the various array elements:

for(i=0; i<numberOfPeople; ++i)
   {
   printf("Enter name #%d: ", i);
   fgets(tmpStr, sizeof(tmpStr), stdin);

Now, allocate memory, and store the entered name in the names array.

   names[i] = strdup(tmpStr);

The 'strdup()' function calles 'malloc()' internally, allocating just enough memory to store the specified string (tmpStr); then it copies the string (tmpStr) to the allocated memory, and returns the address of the allocated memory; which is assigned to 'names[i]'.

Then get the age from the user:

   printf("Enter age #%d: ", i);
   fgets(tmp, sizeof(tmpStr), stdin);

   ages[i] = atoi(tmpStr);

Then get ...

...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top