GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Mar-2010, 14:40
kingnuddy kingnuddy is offline
New Member
 
Join Date: Feb 2010
Posts: 25
kingnuddy is an unknown quantity at this point

Array of Structures


What is wrong with this. The compiler returns
(expected `}' before '{' token )
(expected `,' or `;' before '{' token)

CPP / C++ / C Code:
#include <stdio.h>

struct customer {
 char firm[20];
 char contact[25];
 };

 struct sale {
 struct customer buyer;
 char item[20];
 float amount;
 };


 struct sale y1990[100] = {
 { { "Acme Industries", "George Adams"},
 "Left-handed widget",
 1000.00
 }
 { { "Wilson & Co.", "Ed Wilson"},
 "Type 12 gizmo",
 290.00
 }
 };
Last edited by admin : 18-Mar-2010 at 19:25. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 17-Mar-2010, 16:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Array of Structures


Quote:
Originally Posted by kingnuddy
What is wrong with this....
In initializer lists, each element in the array initializer is enclosed in braces. Array element initializers are separated by commas.

Struct initializers are enclosed in braces, and the individual members are separated by commas.


For your array, I'll put each element's initializer on a separate line. (Of course it doesn't make any difference to the compiler, but we mere humans sometimes benefit from clear delineations like this.)
CPP / C++ / C Code:
#include <stdio.h>

struct customer {
    char firm[20];
    char contact[25];
};

struct sale {
    struct customer buyer;
    char item[20];
    float amount;
};


int main()
{
    struct sale y1990[100] = {
        {{"Acme Industries", "George Adams"}, "Left-handed widget", 1000.00}, /* element zero */
        {{"Wilson & Co.",       "Ed Wilson"}, "Type 12 gizmo",       290.00}  /* element one  */
    };
    int i;

    for (i = 0; i < 2; i++) {
        printf("y1990[%d]:\n", i);
        printf("  %-19s  %-24s\n", 
                y1990[i].buyer.firm, y1990[i].buyer.contact);
        printf("    %-20s: %7.2f\n", y1990[i].item, y1990[i].amount);
        printf("\n");
    }

    return 0;
}

Output:
Code:
y1990[0]: Acme Industries George Adams Left-handed widget : 1000.00 y1990[1]: Wilson & Co. Ed Wilson Type 12 gizmo : 290.00


Regards,

Dave
Last edited by davekw7x : 17-Mar-2010 at 17:32.
  #3  
Old 18-Mar-2010, 06:47
kingnuddy kingnuddy is offline
New Member
 
Join Date: Feb 2010
Posts: 25
kingnuddy is an unknown quantity at this point

Re: Array of Structures


Thanks alot Dave. Appreciate it alot
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 15:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 16:16
How Do I Create a Function that returns a usable array of pointers to structures? UncleRic C Programming Language 2 03-Sep-2007 11:17
Sort an array of structures sassy99 C Programming Language 10 15-Feb-2007 07:46
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:02.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.