CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2002
    Posts
    1

    explicitly defining each member of an array of structs

    How would I make an array of structs then explicitly define each member of each struct in the array? For example:

    Code:
    struct test
    { 
         char member1;
         char member2;
    };
    
    typedef test array[2];
    
    array real_var={
    { a, b }
    { c, d }
    };
    This obviously isn't right, any help or suggestions?

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Hello there. You are very close:
    Code:
    struct test
    { 
         char member1;
         char member2;
    };
    
    typedef test array[2];
    
    array real_var={
    { 'a', 'b' },
    { 'c', 'd' }
    };
    To initialize a struct, you just initialize each of its members in a
    comma-separated list ... all within curly brackets. The same thing
    is done with arrays. That's why you see a comma between the
    first struct and the second struct.

    The only other problem you had was not putting apostrophes
    around your char's. The way to specify a char data type is to
    put apostrophes around them; that's why you see 'a', 'b', etc....

    --Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured