0

This is my code:

struct first
{
    char x;
    int y;
};
first a[3]={{'a',1},{'c',2},{'b',3}};
struct second
{
    first b[2]; 
    int z;
};
second c={{a[0],a[1]},12};

Basically, when I'm assigning the second structure, the first element is supposed to be an array of the first structure type. So I am trying to put two elements a[0] and a[1] in it. But it shows the error:

ERROR CYAPA.CPP 12: Cannot convert 'first' to 'char'

ERROR CYAPA.CPP 12: Cannot convert 'first' to 'int'

What am I doing wrong? I am using Turbo c++ as it is what is allowed by our curriculum in India.

3
  • 1
    On gcc 4.8, the code compiled (I puh both initializations inside main()). Commented May 13, 2016 at 11:24
  • 1
    lol. You're required to use Turbo C++. What a shame. :( Commented May 13, 2016 at 11:26
  • Thanks for the input. I ultimately ended up creating an 'id' for each element of the first structure, and just use an integer in the second structure to access a particular index of a[]. Commented May 13, 2016 at 11:46

1 Answer 1

1

This sort of initialization is not allowed in C, but it was allowed in C++98 .

Your compiler predates 1998 by several years so it is not surprising that it does not allow some things that did become part of the C++ Standard.

You'll have to write {'a', 1} instead of a[0] etc. , or use a macro. The macro solution might look like:

#define A0 {'a', 1}
#define A1 {'c', 2}
#define A2 {'b', 3}

first a[3]={A0, A1, A2};
second c={{A0, A1}, 12 };

Alternatively you could initialize a and then set up c at runtime.

Sign up to request clarification or add additional context in comments.

1 Comment

Sad that my school uses such ancient standards... I'll definitely ask our Teacher to upgrade!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.