3

I have

#include <iostream>

typedef struct coordinate{
    double x;
    double y;
}point;

typedef struct sc_cell{ // single cell
    point sc[4];
}cell;

typedef struct sb_body { // for single body
    point sb[4];
}body;

using namespace std;

int main()
{
    body r_plate = {};
    
    r_plate.sb[0] = { 0,0 };
    r_plate.sb[1] = { 5,0 };
    r_plate.sb[2] = { 5,1 };
    r_plate.sb[3] = { 0,1 };


    return 0;
}

Here, in main I have initialized the r_plate with 4 points and took 4 lines. is there any way to initialize it in a single line?

Something like r_plate = { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } }(this shows error too many initializer values)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

2 Answers2

2

The structure body is an aggregate that contains data members that in turn are aggregates.

You need to write

body r_plate = { { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } } };

That is the structure body contains an array so you have to write

body r_plate = { { ... } };

and each element of the array is an object of the structure type. So you will have

body r_plate = { { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } } };

The following initializations will be less readable but correct

body r_plate = { { 0,0,5,0,5,1,0,1 } };

and

body r_plate = { 0,0,5,0,5,1,0,1 };

Here is a demonstration program.

#include <iostream>

typedef struct coordinate{
    double x;
    double y;
}point;

typedef struct sc_cell{ // single cell
    point sc[4];
}cell;

typedef struct sb_body { // for single body
    point sb[4];
}body;

using namespace std;

int main()
{
    body r_plate = { 0,0,5,0,5,1,0,1 };

    for ( const auto &p : r_plate.sb )
    {
        std::cout << "( " << p.x << ", " << p.y << " ) ";
    }
    std::cout << '\n';

    r_plate = { { 0,0,5,0,5,1,0,1 } };

    for ( const auto &p : r_plate.sb )
    {
        std::cout << "( " << p.x << ", " << p.y << " ) ";
    }
    std::cout << '\n';

    r_plate = { { { 0,0 }, { 5,0 } , { 5,1 }, { 0,1 } } };

    for ( const auto &p : r_plate.sb )
    {
        std::cout << "( " << p.x << ", " << p.y << " ) ";
    }
    std::cout << '\n';

    return 0;
}

The program output is

( 0, 0 ) ( 5, 0 ) ( 5, 1 ) ( 0, 1 ) 
( 0, 0 ) ( 5, 0 ) ( 5, 1 ) ( 0, 1 ) 
( 0, 0 ) ( 5, 0 ) ( 5, 1 ) ( 0, 1 ) 

As for this assignment

r_plate = { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } };

then the first inner brace is considered as starting point of the list-initialization of the array. As the structure has only one data member (the array) then all other list-initializations apart the first one do not have corresponding data members of the structure. So the compiler issues an error.

halfer
  • 19,824
  • 17
  • 99
  • 186
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

just one more { }. One for the body, one for the array and then one for the point struct.

body r_plate = { { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } } };

https://godbolt.org/z/WKz1zf6Kn

You can split them:

for the body:

body bd = {0,0};

for an array of body:

body bd_arr[4] = { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } };

and then for the struct containing an array of body:

body r_plate = { { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } } };
mch
  • 9,424
  • 2
  • 28
  • 42