Jack Morton
Jack Morton

Reputation: 199

array of pointers to structures

I'm trying to understand if my code is correct. I need to declare an array of pointers to structs, create a new struct and assign the values and print them. It seems to me that I'm not declaring array of pointers correctly. I need to know what I'm doing wrong. Thank you I'm getting this compile error: error: 'people' undeclared (first use in this function) And I've tried to insert struct data *list; into main but it wouldnt work

     char *book[] = { "x", "y", "z",};
     int number[] = { 1, 2, 3};

     struct data = { char *bookname; int booknumber;};

     function(char *x, int y)
     {
       static int count;

       struct data *list[3];

       //creating a new struct 
       list[count] = (struct data*) malloc( sizeof(struct data) );

       //assigning arguments
       list->bookname = x;
       list->booknumber = y;

       count++;
     }

     int main()
     {
       struct data *list[3];

       int i;
       for(i = 0; i < 3; i++)
       {
         function(book[i], number[i]);

         printf("name: %c number: %d", list[i]->bookname, list[i]->booknumber);
       }

Upvotes: 4

Views: 28922

Answers (4)

Jay
Jay

Reputation: 24915

Please change the following piece of code

    // declaring array of pointers to structs //         
     struct data *list;         
    //not compiling        
    //struct data *list[3]; ---> There is no problem with this statement.        
   //creating a new struct         
   list = (struct data*) malloc( sizeof(struct data) );  ---> //This statement should compilation error due to declaration of struct data *list[3]

to

struct data *list[100]; //Declare a array of pointer to structures  
//allocate memory for each element in the array
list[count] = (struct data*) malloc( sizeof(struct data) ); 

Upvotes: 2

neel
neel

Reputation: 9071

These are things are wrong in your program

struct data = { char *bookname; int booknumber;};

"=" should not be there

list = (struct data*) malloc( sizeof(struct data) );
list[count]->bookname = x;
list[count]->booknumber = y;

Here you are creating space for single list, so you cant do list[count]-> bookname, it should be list->bookname. Same with booknumber
And list is local to function, you cant access it in main.

Upvotes: 0

Geoffrey R.
Geoffrey R.

Reputation: 2116

I think you should write:

char *book[] = { "x", "y", "z"};

Because in your case you were declaring an array of chars and filling it with pointers, which actually makes no sense.

In the line of code above, it just means "declare an array of pointers".

Hope it helped...

Upvotes: 0

chrisaycock
chrisaycock

Reputation: 37938

Since you want arrays, you need to declare arrays:

char *book[] = { "x", "y", "z",};
int number[] = { 1, 2, 3};

Another issue is

list = (struct data*) malloc( sizeof(struct data) );

//assigning arguments
list[count]->bookname = ...

Here, list is always going to have exactly one element. So if count is anything other than 0, you will be accessing an array out of bounds!

Upvotes: 3

Related Questions