Thread: array inside struct

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    32

    array inside struct

    im having a problem getting this to work. the array....
    Code:
    struct airline {
            int seats[11] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 } ;
           char  * customer;
    
    };
    this is what i get:
    Code:
    9 C:\Dev-Cpp\airlines.c [Warning] no semicolon at end of struct or union 
    9 C:\Dev-Cpp\airlines.c syntax error before '=' token 
    12 C:\Dev-Cpp\airlines.c syntax error before '}' token
    when i try int seats[11]; it works fine..
    what can i do? thx

  2. #2
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Code:
    struct airline {
            int seats[11] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 } ;
           char  * customer;
    
    };
    You are creating a new data type first. Onces you've told your compiler what that new data type is all about, you can create a variable of that new type and initialized it. If you want to go even the extreme, you can declare that new variable, right after the definition of that new type:

    Code:
    struct airline{
        int seats[11];
        char  *customer;
    }flight = { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 } }; /* This is a partial initialization since nothing is done for the pointer customer */
    BTW, if you want to store 12 elements in the array; the subscript should be the same amount.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    ok i think i solved the problem
    what i did
    Code:
    struct airline {
            int seats[12] ;
           char  customer;
    };
    
    int main(int argc, char *argv[])
    {
        struct airline seat[12] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
    this seemes to work just fine! =)

  4. #4
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by tat View Post
    this seemes to work just fine! =)
    It only seems. You are doing something different that what you intended at first.
    You are declaring an array of 12 elements of type struct airline; name seat.

    If you want to initialize the int seats inside the structure:
    Code:
    struct airline seat = { { 1,2,3,4,5,6,7,8,9,10,11,12 } };
    [Edit] However if you want to declare a structure for every seat out of 12 seats then that's what you have.
    Last edited by Aia; 11-12-2007 at 09:51 PM. Reason: Extra though

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to separate declarations and definitions, initializations.
    First, you must tell the compiler how the actual struct looks like - ie, no initialization. Just tell the compiler how the struct looks like, how the array looks like, and so on.
    When creating an object of that struct type, THAT is where you need to put the data you want into the struct.
    I hope that makes sense, because it's exactly what Aia did.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    Quote Originally Posted by Elysia View Post
    You need to separate declarations and definitions, initializations.
    First, you must tell the compiler how the actual struct looks like - ie, no initialization. Just tell the compiler how the struct looks like, how the array looks like, and so on.
    When creating an object of that struct type, THAT is where you need to put the data you want into the struct.
    .
    is this what you mean?
    Code:
    struct airline {
            int seats[12];
           char  customer[BUFSIZ];
    };
    
    
    char showmenu ( void );  // proto functions  //
    void shownumber ( struct airline nos );
    
    int main(int argc, char *argv[])
    {
        struct airline seats[12] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
    also i want to pass in the array of seats to a function so i can print it out!
    how can i do this.. it dont work i get a
    error of
    31 C:\Dev-Cpp\airlines.c incompatible type for argument 1 of `shownumber'
    Code:
    struct airline {
            int seats[12];
           char  customer[BUFSIZ];
    };
    
    /**********************************************/
    char showmenu ( void );  // proto functions  //
    void shownumber ( struct airline nos );
    
    /*******************************************/
    
    /******************** MAIN******************************/
    int main(int argc, char *argv[])
    {
        struct airline seats[12] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
    
           
        char choice;
        
    while ( ( choice = showmenu() )!= 'f' )    // loop for menu
        {
            switch ( choice )
            {
              case 'a' : shownumber ( seats );
                   break;
              
              case 'b' : printf(" you hit b");
                   break;
              }
      
      system("PAUSE");	
      return 0;
    }
    }
    /*********************** MENU FUNCTION *****************************/
    
    char showmenu ( void )  
    {
      char ans;
      printf(" ********** Welcome to Swift Lines Airlines ************\n");
      printf("\n ********** To Choose A Function, Enter its Letter Label:\n");
      printf("\na ) Show Number of seats\n");
      printf("b ) Show List of seats\n");
      printf("c ) Show alphabetical list of seats\n");
      printf("d ) Assign a Customer to a seat assignment\n");
      printf("e ) Delete a seat assignment\n");
      printf("f ) Quit\n");
      
      ans = getchar();
      ans = tolower( ans );
      
      while ( strchr ( "abcdef" , ans ) == NULL )
      {
            puts( " please pick a - f :\n");
            ans = tolower( getchar() );
            }
      return ans;
    }     
    /********************** END MENU FUNCTION *************************/
      
    void shownumber ( struct airline nos[] )
    {
         int x;
         for ( x = 0 ; x < 12; X++ )
         {
       printf ( " swift Lines seats-- %d" , nos->seats );
    }
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, first do you want an actual array of the struct or simply initialize the array INSIDE the struct? You're creating 12 instances of the struct currently.
    Also, take a look at your declaration of the shownumber function. It doesn't match with your definition.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    Quote Originally Posted by Elysia View Post
    OK, first do you want an actual array of the struct or simply initialize the array INSIDE the struct? You're creating 12 instances of the struct currently.
    Also, take a look at your declaration of the shownumber function. It doesn't match with your definition.
    i wanted to initialize the array inside the struct then pass that array to a function to print it..

  9. #9
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by tat View Post
    i wanted to initialize the array inside the struct then pass that array to a function to print it..
    Code:
    #include <stdio.h>
    
    struct airline {
            int seats[12] ;
           char  customer;
    };
    
    void printing( int *seat, int size );
    
    int main(int argc, char *argv[])
    {
        struct airline seat = { { 1,2,3,4,5,6,7,8,9,10,11,12 } };
        
        printing( seat.seats, sizeof seat.seats / sizeof ( int ) );
        
        
        getchar();
        return 0;
     
    }
    void printing( int *seat, int size )
    {
    	int i = 0;
    	
    	for ( i = 0; i < size; i++ )
    	{
    		printf( "%d ", *( seat + i ) );
    	}
    }
    /* output:
    1 2 3 4 5 6 7 8 9 10 11 12 */

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    IF you have a struct, then I don't see why you shouldn't pass the entire struct instead of passing all members separately. And since there's only ONE struct with a static array, there's no need to pass the size of the struct either.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    Aia - - - brilliant
    thx

    ohhh would this be considered a string?
    Code:
    struct airline seat  = { { 1,2,3,4,5,6,7,8,9,10,11,12 }};
    
    and thats why you did this?
    void printing ( int *seat );
    dumb Q i know
    Last edited by tat; 11-12-2007 at 10:41 PM.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    Quote Originally Posted by Elysia View Post
    IF you have a struct, then I don't see why you shouldn't pass the entire struct instead of passing all members separately.
    I tried to do that..

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tat View Post
    Aia - - - brilliant
    thx

    ohhh would this be considered a string?
    Code:
    struct airline seat  = { { 1,2,3,4,5,6,7,8,9,10,11,12 }};
    
    and thats why you did this?
    void printing ( int *seat );
    dumb Q i know
    That's wrong. Pointer should be the same type as the the struct. You shouldn't needlessly pass a pointer of type airline to a function that wants int*.
    Change your function:
    Code:
    void printing(struct airline* seat);
    That is, if you want to pass the entire struct.
    Last edited by Elysia; 11-12-2007 at 10:57 PM.

  14. #14
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by tat View Post
    Aia - - - brilliant
    No really. Just a pointless exercise.

    Quote Originally Posted by tat View Post
    ohhh would this be considered a string?
    Code:
    struct airline seat  = { { 1,2,3,4,5,6,7,8,9,10,11,12 }};
    No. A string is an array of type char that terminates with the '\0' character.
    This is just an array of integers.
    Quote Originally Posted by tat View Post
    and thats why you did this?
    Code:
    void printing ( int *seat );
    printing accepts a pointer to an int.
    passing seat.seats to the function printing is like passing the address of seat.seats[0]
    I could have written: void printing( int seat[] )
    Last edited by Aia; 11-12-2007 at 10:57 PM.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    12 seats, and only one passenger?

    Surely you want
    Code:
    struct airline {
            int seat;
           char  customer[100];
    };
    
    // Now
    struct airline seats[12];
    
    Then you can do things like
    seats[0].seat = 4;
    strcpy( seats[0].customer, "Fred Flintstone");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  4. Struct inside a Struct inside a Struct?!?
    By tegwin in forum C++ Programming
    Replies: 8
    Last Post: 05-02-2003, 11:50 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM