36

I'm trying to create an array of structs. Is the code below valid? I keep getting an expected primary-expression before '{' token error.

int main() {
  int pause;
  struct Customer {
    int uid;
    string name;
  };

  Customer customerRecords[2];
  customerRecords[0] = {25, "Bob Jones"};
  customerRecords[1] = {26, "Jim Smith"};
  cin >> pause;
  return 0;
}
2
  • 1
    Your example compiles without errors on ideone (gcc-4.5.1) ideone.com/c9kMr Commented Jul 25, 2011 at 0:24
  • 5
    This is because of new features inherent to initialization lists and C++0x ... his code does not compile under C++03. Commented Jul 25, 2011 at 0:37

5 Answers 5

72

Try this:

Customer customerRecords[2] = {{25, "Bob Jones"},
                               {26, "Jim Smith"}};
Sign up to request clarification or add additional context in comments.

Comments

35

You can't use an initialization-list for a struct after it's been initialized. You've already default-initialized the two Customer structs when you declared the array customerRecords. Therefore you're going to have either use member-access syntax to set the value of the non-static data members, initialize the structs using a list of initialization lists when you declare the array itself, or you can create a constructor for your struct and use the default operator= member function to initialize the array members.

So either of the following could work:

Customer customerRecords[2];
customerRecords[0].uid = 25;
customerRecords[0].name = "Bob Jones";
customerRecords[1].uid = 25;
customerRecords[1].namem = "Jim Smith";

Or if you defined a constructor for your struct like:

Customer::Customer(int id, string input_name): uid(id), name(input_name) {}

You could then do:

Customer customerRecords[2];
customerRecords[0] = Customer(25, "Bob Jones");
customerRecords[1] = Customer(26, "Jim Smith");

Or you could do the sequence of initialization lists that Tuomas used in his answer. The reason his initialization-list syntax works is because you're actually initializing the Customer structs at the time of the declaration of the array, rather than allowing the structs to be default-initialized which takes place whenever you declare an aggregate data-structure like an array.

Comments

9

Some compilers support compound literals as an extention, allowing this construct:

Customer customerRecords[2];
customerRecords[0] = (Customer){25, "Bob Jones"};
customerRecords[1] = (Customer){26, "Jim Smith"};

But it's rather unportable.

3 Comments

It's in C99, so hardly "unportable" (depending on whether you want to support antique compilers or not). I prefer it in C to MakeCustomer(25, "Foo") which somehow generates worse code with GCC 4.2. I can't remember if it's in C++; I'm pretty sure (Customer){.uid=25} is not (though it may be in C++0x).
@tc.: MSVC traditionally neglects C99; is MSVC 2008 an antique? (2010 is a little better.)
If after over a decade, they can't be bothered, I'd say so. Then again, I consider most Windows C code "legacy" in terms of style, if nothing else.
9

It works perfectly. I have gcc compiler C++11 ready. Try this and you'll see:

#include <iostream>

using namespace std;

int main()
{
    int pause;

    struct Customer
    {
           int uid;
           string name;
    };

    Customer customerRecords[2];
    customerRecords[0] = {25, "Bob Jones"};
    customerRecords[1] = {26, "Jim Smith"};
    cout << customerRecords[0].uid << " " << customerRecords[0].name << endl;
    cout << customerRecords[1].uid << " " << customerRecords[1].name << endl;
    cin >> pause;
return 0;
}

Comments

0

you can use vector. First Define the Struct.

  struct Customer {
    int uid;
    string name;
  };

Then,

vector<Customer> array_of_customers;

By using vector, you will have more freedom and access in the array of structure.

Now if want to add an struct element in the define array. You can use

array_of_customer.push_pack(/* struct element here */)

Example:

Customer customer1;
customer1.uid = 01;
customer1.name = "John Doe";

Customer customer2;
customer2.uid = 02;
customer2.name = "Blah Blah";


array_of_customers.push_back(customer1);
array_of_customers.push_back(customer2);

you have now an array of struct.

Comments