Thread: Arrays of Structures in a Structure

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    5

    Question Arrays of Structures in a Structure

    Hi,

    I haven't programed in C for a long time and I have decieded on a simple project to get myself back into it. However, it seems mot so simple!

    I want to create a list of pupil details in a number of classes. So far i've got:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct {
    	char * name;
    	int age;
    } pupil;
    
    typedef struct {
    	char * name;
    	int num_pupils;
    	struct pupil ** pupils;
    } class;
    
    
    int main ()
    {
    	class * class1;
    	
    	class1 = (class *)calloc(sizeof(class),1);
    	class1->name = (char *)calloc(sizeof(char),128);
    
    	strcpy (class1->name, "Hello");
    	class1->num_pupils = 20;
    	printf ("Class %s has %d pupils\n", class1->name, class1->num_pupils);
    
    	class1->pupils = (pupil*)calloc(sizeof(pupil), 20);
    }
    I think i've made a mistake on the last line as this is where it fails to compile but there may be others, i'd apreciate any help on the current problem and any changes which "should" be made to the code.

    Thanks for any help

  2. #2
    Quote Originally Posted by DTM
    I haven't programed in C for a long time and I have decieded on a simple project to get myself back into it. However, it seems mot so simple!

    I want to create a list of pupil details in a number of classes. So far i've got:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct {
    	char * name;
    	int age;
    } pupil;
    
    typedef struct {
    	char * name;
    	int num_pupils;
    	struct pupil ** pupils;
    } class;
    
    int main ()
    {
    	class * class1;
    	
    	class1 = (class *)calloc(sizeof(class),1);
    You don't need this cast. also, calloc() will stick all the bits to 0 which is not guaranteed to set the pointers to the null constant pointer (aka NULL). Finally, *alloc() can fail. You must test the returned value.
    Code:
    	class1->name = (char *)calloc(sizeof(char),128);
    sizeof (char) is 1 by-definition.
    Code:
    	strcpy (class1->name, "Hello");
    	class1->num_pupils = 20;
    	printf ("Class %s has %d pupils\n", class1->name, class1->num_pupils);
    
    	class1->pupils = (pupil*)calloc(sizeof(pupil), 20);
    The cast is wrong. Just drop it. You want
    Code:
    	class1->pupils = malloc (sizeof *class1->pupils * 20); /* fixed */
    Then, you have to set the 20 pointers to NULL, and to record the pupil's address... But you know that (I think).
    Code:
    }
    Don't forget to free the memory when you don't use it any more.

    This little project would be a good support to implement some smart ADTs (Abstract Data Types).
    Last edited by Emmanuel Delaha; 08-09-2004 at 07:16 AM. Reason: Code fixed
    Emmanuel Delahaye

    "C is a sharp tool"

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    Thanks for that,

    I've tried to compile the code again but get the error message:

    "invalid operands to binary * "

    for the last line:

    Code:
    	class1->pupils = malloc (sizeof *class1->pupils) * 20;

  4. #4
    Quote Originally Posted by DTM
    Thanks for that,

    I've tried to compile the code again but get the error message:

    "invalid operands to binary * "

    for the last line:

    Code:
    	class1->pupils = malloc (sizeof *class1->pupils) * 20;
    Because it's darn wrong! I meant:

    Code:
    	class1->pupils = malloc (sizeof *class1->pupils * 20);
    (Original fixed too)
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're trying to multiply malloc()'s return address by 20. This is bad. Move the *20 inside the malloc() function:
    class1->pupils = malloc(sizeof(*class1->pupils)*20);
    If you understand what you're doing, you're not learning anything.

  6. #6
    Quote Originally Posted by itsme86
    You're trying to multiply
    Actually, I was trying. Code fixed.
    Emmanuel Delahaye

    "C is a sharp tool"

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    Thanks
    Last edited by DTM; 08-09-2004 at 12:48 PM.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    Maybe it's been too long since i've coded in C but the code for assigning the adress of the pupils should look like:

    Code:
    	pupil * pupil_1;
    
    	pupil_1 = malloc (sizeof(pupil);
    
    	class1->pupil[0] = &pupil_1;
    or have i done something stupid?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    class1->pupil[0] is a pointer. As such, you just assign it the pointer directly:
    Code:
    pupil * pupil_1;
    
    pupil_1 = malloc( sizeof *pupil_1 );
    class1->pupil[0] = pupil_1;
    Assuming you've allocated your class1->pupil "array".

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    That all seems to make sense but i get the error message:

    "defrencing pointer to incomplete type"

    any suggestions?

  11. #11
    Quote Originally Posted by DTM
    Maybe it's been too long since i've coded in C but the code for assigning the adress of the pupils should look like:
    Code:
    	pupil * pupil_1;
    
    	pupil_1 = malloc (sizeof(pupil);
    
    	class1->pupil[0] = &pupil_1;
    or have i done something stupid?
    I leave the comments on your own...

    A pointer is a variable that holds an address. You just want to copy the value.
    Code:
    {
       pupil * pupil_1 = malloc (sizeof *pupil_1);
    
       if (pupil_1 != NULL)
       {
          class1->pupil[0] = pupil_1;
       }
    }
    Emmanuel Delahaye

    "C is a sharp tool"

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since you didn't post any code as to your latest error, and since you apparently didn't grasp the intent of my pervious post:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct foo;
    struct bar;
    
    struct foo {
            struct bar**b;
    };
    
    struct bar
    {
            int x;
    };
    
    int main( void )
    {
            struct foo f, *p = &f;
            struct bar *b;
            int x;
    
            p->b = malloc( sizeof *b * 3 );
            for( x = 0; x < 3; x++ )
            {
                    b = malloc( sizeof *b );
                    p->b[x] = b;
            }
    
            for( x = 0; x < 3; x++ )
            {
                    free( p->b[x] );
            }
            free( p->b );
    
            return 0;
    
    }
    You get the lazy version, because I didn't feel like mallocing and freeing foo.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    p->b = malloc( sizeof *b * 3 );
    I think you meant:
    Code:
    p->b = malloc( sizeof *p->b * 3 );
    Code:
     struct foo;
     struct bar;
    You don't need to declare these two here (just saving those typing fingers! )
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Hammer
    Code:
    p->b = malloc( sizeof *b * 3 );
    I think you meant:
    Code:
    p->b = malloc( sizeof *p->b * 3 );
    :doh:

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-13-2003, 10:53 PM
  2. structures and arrays
    By cnewbee in forum C Programming
    Replies: 1
    Last Post: 04-01-2003, 01:01 PM
  3. returning arrays of structures
    By manolo21 in forum C Programming
    Replies: 2
    Last Post: 03-31-2003, 08:09 AM
  4. Working with arrays of structures, need advice...
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-14-2002, 08:38 AM
  5. errors with arrays and structures
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 11:48 PM