Daniel Jaló
Daniel Jaló

Reputation: 157

Array inside a structure

I want to create a structure that holds arrays with fixed size inside:

struct smt{
   int array1[3];
   int array2[10];
   int bananas;
};

So that I can use it in my main code. However, when I try to fill the arrays I always get an error:

int main(){
   smt name;
   name.array1 = {1,2,3};

   return 0;
}

The errors are on the name.array1 = {...}; line:

error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'

Any help would be appreciated. I've tried to find similar problems but haven't found anything helpful so far.

Upvotes: 5

Views: 370

Answers (4)

Davidbrcz
Davidbrcz

Reputation: 2397

With C++11 uniform initialiser list, you can do this (It would also work in C++03 because smt is POD).

#include <iostream>
using namespace std;

struct smt{
  int array1[3];
  int array2[10];
  int bananas;
};

int main() {
//I use {} to do a value-initialization (to zero) of array2
smt s={{0,1,2},{},5};

std::cout<<s.array1[0]<<std::endl;
return 0;
}

Moreover, using a std::array enables you do to so :

std::array<int,4> a;
a={1,2,3,4};

Because there is a default assignment operator for std::array generated by the compiler.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311146

This statement

name.array1 = {1,2,3};

is not an initialization during defining an object. It is an assignment statement. But arrays have no assignment operator.

You could initialize your array during creating of an object of the structure. For example

smt name = { { 1, 2, 3 } };

Or if you indeed need to assign 1, 2, 3 to elements of the array you could use standard algorithm std::iota. For example

std::iota( std::begin( name.array1 ), std::end( name.array1 ), 1 );

Or you could assign each element of the array separatly as for example

name.array1[0] = 1;

That to use the syntax you originally wanted you should substitute array definitions for std::array. For example

#include <array>
struct smt{
   std::array<int, 3> array1;
   std::array<int, 10> array2;
   int bananas;
};

In this case such code as

int main(){
   smt name;
   name.array1 = {1,2,3};

   return 0;
}

will be valid provided that your compiler supports the C++ 2011 Standard.

Upvotes: 0

OlivierLi
OlivierLi

Reputation: 2846

The {...} syntax can only be used during initialization.

You need to initialize your struct all at once to use this syntax.

For exemple :

struct smt {
  int array1[3];
  int array2[10];
  int bananas;
};

struct smt mystruct = { 
   { 1, 2, 3 },
   { 1, 2 ,3 ,4 5, 6, 7, 8, 9, 10 },
   0
};

Also note that in C99 (but not C++) you could use designated initializers like this to improve readability :

struct smt mystruct = { 
   .array1  = { 1, 2, 3 },
   .array2  = { 1, 2 ,3 ,4 5, 6, 7, 8, 9, 10 },
   .bananas = 0
};

Upvotes: 4

Maroun
Maroun

Reputation: 96016

You can't do it like this if it's not in the initialization. You should do:

name.array1[0] = 1;
name.array1[1] = 2;
name.array1[2] = 3;

See this helpful answer:

It's not just arrays, you cannot provide an initializer for anything at any point other than in a definition. People sometimes refer to the second statement of something like int i; i = 0; as "initializing i". In fact it's assigning to i, which previously holds an indeterminate value because it wasn't initialized. It's very rarely confusing to call this "initializing", but as far as the language is concerned there's no initializer there.

Upvotes: 5

Related Questions