why can not assign value to an array variable of a structure?

B

baumann@pan

hi all,


#include "stdafx.h"

struct a_{
int a;
int b;
};

struct a_ a1 ,a2, a3, a4, a5, a6;
#if 0
struct a_ m[6] = { a1,a2,a3,a4,a5,a6};
#endif
int a[6] = { 1, 2, 3, 4, 5, 6};

int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}


the above code tells me struct a_ m[6] ={ }; statement is wrong,

but why int a[6] ={}; is ok?

i know if i write struct a_ m[6] = {
{a1.a,a1.b},{a2.a,a2.b},{a3.a,a3.b},{
a4.a,a4.b},{a5.a,a5.b}}; it will be ok, i have not test the idea.
 
A

Arun

struct a_ m[6] = { a1,a2,a3,a4,a5,a6};

initialization elements must be constants which in this case (a1..a6)
are not.

-ASJ
 
C

Chris \( Val \)

| hi all,
|
|
| #include "stdafx.h"
|
| struct a_{
| int a;
| int b;
| };
|
| struct a_ a1 ,a2, a3, a4, a5, a6;
| #if 0
| struct a_ m[6] = { a1,a2,a3,a4,a5,a6};
| #endif
| int a[6] = { 1, 2, 3, 4, 5, 6};
|
| int main(int argc, char* argv[])
| {
| printf("Hello World!\n");
| return 0;
| }
|
|
| the above code tells me struct a_ m[6] ={ }; statement is wrong,
|
| but why int a[6] ={}; is ok?

It might be a compiler bug?
It works fine with MinGW.

| i know if i write struct a_ m[6] = {
| {a1.a,a1.b},{a2.a,a2.b},{a3.a,a3.b},{
| a4.a,a4.b},{a5.a,a5.b}}; it will be ok, i have not test the idea.

The following should work:
a_ m[6] = { {5,10}, {15,20}, {25,30}, {35,40}, {45,50}, {55,60} };

Btw, you don't need the 'struct' keyword in the declaration
in C++.

Cheers,
Chris Val
 
J

John Carson

baumann@pan said:
hi all,


#include "stdafx.h"

struct a_{
int a;
int b;
};

struct a_ a1 ,a2, a3, a4, a5, a6;
#if 0
struct a_ m[6] = { a1,a2,a3,a4,a5,a6};
#endif
int a[6] = { 1, 2, 3, 4, 5, 6};

int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}


the above code tells me struct a_ m[6] ={ }; statement is wrong,

but why int a[6] ={}; is ok?

i know if i write struct a_ m[6] = {
{a1.a,a1.b},{a2.a,a2.b},{a3.a,a3.b},{
a4.a,a4.b},{a5.a,a5.b}}; it will be ok, i have not test the idea.


It compiles without complaint on VC++ 7.1 and Comeau.
 
B

baumann@pan

i am using VC++ 6, not the one u use.

does the standard of C99 /C++ guide what we should do under this case?
 
B

baumann@pan

i tried with vs.net 2003 studio, it works, but VC++ 6.0 really fails to
compile the code.
 
J

Jack Klein

i am using VC++ 6, not the one u use.

does the standard of C99 /C++ guide what we should do under this case?

The standard of C99 is not relevant to C++. The C++ standard is based
in part on the C standard as of 1995.

As for the C++ standard, it suggests that you use a compiler that
conforms to the C++ standard. Microsoft Visual C++ 6.0 does not.
 
B

baumann@pan

The standard of C99 is not relevant to C++. The C++ standard is based
in part on the C standard as of 1995.

As for the C++ standard, it suggests that you use a compiler that
conforms to the C++ standard. Microsoft Visual C++ 6.0 does not.

according to your view, if the compiler conforms to the C++ standard,
the code should work? right? thanks
 
A

Achintya

Hi,

As mentioned by Mr. Arun also, this kind of array initialization
requires constant values.
Since the struct are already initialized to zero( static variable
initilization) in the previous statement i.e

struct a_ a1 ,a2, a3, a4, a5, a6;

...those values are considered for initialization in the array of
structs if you use...

struct a_ m[6] =
{{a1.a,a1.b},{a2.a,a2.b},{a3.a,a3.b},{a4.a,a4.b},{a5.a,a5.b}};

....and if you use....

a_ m[6] = {10,20,30,40,50,60};

....the initialization is m[0].a=10, m[0].b=20,
m[1].a=30....m[4].a=0...etc...

I have tried on VC++ 6.0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top