2

Have to create an array of struct. Since don't know how many entries will have in the array, have to use dynamic array.

Did not worked as advised here - https://stackoverflow.com/a/8455125/1090944.

Having troubles with passing array of struct to function for allocation.

Here is the code:

#include<stdio.h>
#include<stdlib.h>
struct record
{
 char * community_name;
 double data[10];
 double crimes_per_pop;
};
void allocate_struct_array(struct record * array, int length);
int main()
{
 int num_lines = 10;
 struct record ** data_array;
 allocate_struct_array(&data_array, num_lines);
 data_array[0]->community_name[0] = 'h';
 printf("%c\n", data_array[0]->community_name[0]);
 return 0;
}
void allocate_struct_array(struct record * array, int length)
{
 int i;
 *array = malloc(length * sizeof(struct record *));
 if (!array)
 {
 fprintf(stderr, "Could not allocate the array of struct record *\n");
 exit(1);
 }
 for (i = 0; i < length; i++)
 {
 array[i] = malloc( sizeof(struct record) );
 if (!array[i])
 {
 fprintf(stderr, "Could not allocate array[%d]\n", i);
 exit(1);
 }
 array[i]->community_name = malloc(100 * sizeof(char));
 }
}

Here are errors and warnings that I got from the compiler:

../src/temp.c: In function 'main':
../src/temp.c:19: warning: passing argument 1 of 'allocate_struct_array' from incompatible pointer type
../src/temp.c: In function 'allocate_struct_array':
../src/temp.c:32: error: incompatible types in assignment
../src/temp.c:42: error: incompatible types in assignment
../src/temp.c:44: error: wrong type argument to unary exclamation mark
../src/temp.c:50: error: invalid type argument of '->'
make: *** [src/temp.o] Error 1
asked Dec 10, 2011 at 23:56
1
  • The answer there was correct ... you just misinterpreted the implementation -- let me fix THIS code for you with a bit of commentary Commented Dec 11, 2011 at 0:05

2 Answers 2

3

I think you missed the correctness of the original answer, so I will elaborate here...

#include<stdio.h>
#include<stdlib.h>
struct record
{
 char * community_name;
 double data[10];
 double crimes_per_pop;
};
void allocate_struct_array(struct record ** array_pointer, int length);
/* the allocate_struct_array should take a POINTER to a POINTER */
int main()
{
 int num_lines = 10;
/* this is wrong: struct record ** data_array; */
/* it should be: */ struct record *data_array; 
 allocate_struct_array(&data_array, num_lines); /* NOW this will work */
 data_array[0]->community_name[0] = 'h'; /* THIS WILL STILL CRASH community_name is not allocated!!! */
 printf("%c\n", data_array[0]->community_name[0]);
 return 0;
}
void allocate_struct_array(struct record **array_pointer, int length)
{
 int i;
 struct record *array;
 array = malloc(length * sizeof(struct record *));
 if (!array)
 {
 fprintf(stderr, "Could not allocate the array of struct record *\n");
 exit(1);
 }
 for (i = 0; i < length; i++)
 {
 array[i] = malloc( sizeof(struct record) );
 if (!array[i])
 {
 fprintf(stderr, "Could not allocate array[%d]\n", i);
 exit(1);
 }
 }
 *array_pointer = array; /* copied over. */
}

By the way i would like to show you a good C implementation of structure allocations record:

answered Dec 11, 2011 at 0:05
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I copied your version of the code and added array[i]->community_name = malloc(10 * sizeof(char)); to the second loop. But it still does not work.
had to remove the malloc and check parts in allocate_struct_array function, the for loop
1

Your signatures are swapped. In your main function data_array should be declared as a struct record* array,with only one *. Your allocate_struct_array function should instead take struct record** array, with two *'s.

Each * is another layer of indirection. You need to start off with only one layer of indirection in the main function(it is indirect because you are referring to multiple structs with one variable).

You then add another layer of indirection in your allocate_struct_array function, as you need to modify a variable from another function. You can see this adding of the indirection when you use the & operator in your call of allocate_struct_array.

answered Dec 11, 2011 at 0:06

2 Comments

Can I allocate: record ** array, and then in function call as a &(**array), and in the function declaration as record *** array?
Here is complete question - stackoverflow.com/questions/8461512/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.