Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why can not assign value to an array variable of a structure?

Reply
Thread Tools

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

 
 
baumann@pan
Guest
Posts: n/a
 
      05-09-2005
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.

 
Reply With Quote
 
 
 
 
Arun
Guest
Posts: n/a
 
      05-09-2005
> struct a_ m[6] = { a1,a2,a3,a4,a5,a6};

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

-ASJ

 
Reply With Quote
 
 
 
 
Chris \( Val \)
Guest
Posts: n/a
 
      05-09-2005

"baumann@pan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
| 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



 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      05-09-2005
"baumann@pan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com
> 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.

--
John Carson
 
Reply With Quote
 
baumann@pan
Guest
Posts: n/a
 
      05-09-2005
i am using VC++ 6, not the one u use.

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

 
Reply With Quote
 
baumann@pan
Guest
Posts: n/a
 
      05-10-2005
i tried with vs.net 2003 studio, it works, but VC++ 6.0 really fails to
compile the code.

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      05-10-2005
On 9 May 2005 02:55:22 -0700, "baumann@pan" <(E-Mail Removed)>
wrote in comp.lang.c++:

> 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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
baumann@pan
Guest
Posts: n/a
 
      05-10-2005
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

 
Reply With Quote
 
Achintya
Guest
Posts: n/a
 
      05-10-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is it not possible to assign a global variable to anotherglobal variable..? Jase Schick C Programming 11 08-05-2010 05:16 AM
Why can't I assign a class method to a variable? ed Python 3 10-26-2008 12:08 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
assign javascript variable value to VBScript server side variable Matt ASP General 2 09-25-2004 10:40 AM
Can I assign an external value to a JS variable? Harry Stangel Javascript 2 06-08-2004 12:42 AM



Advertisments