473,466 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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.

Jul 23 '05 #1
7 6615
> struct a_ m[6] = { a1,a2,a3,a4,a5,a6};

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

-ASJ

Jul 23 '05 #2

"baumann@pan" <ba*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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

Jul 23 '05 #3
"baumann@pan" <ba*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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
Jul 23 '05 #4
i tried with vs.net 2003 studio, it works, but VC++ 6.0 really fails to
compile the code.

Jul 23 '05 #5
On 9 May 2005 02:55:22 -0700, "baumann@pan" <ba*********@gmail.com>
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
Jul 23 '05 #6
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

Jul 23 '05 #7
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.

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
1
by: luckyyyyyy | last post by:
Hi... actually i want to assign the struct array value after execution...is it possible?...for example: struct data { double x, y, z; }; data objData; i want to assign objData value 1000...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.