473,836 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
+ 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 6631
> 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@pa n" <ba*********@gm ail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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@pa n" <ba*********@gm ail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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@pa n" <ba*********@gm ail.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.l earn.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},{a 2.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
25429
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 the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
26
7108
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 the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field that is in an array? I'm currently getting around the problem by creating a new structure, assign...
19
2663
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 = Color.Empty then..... or if mycolor is Color.Empty then .......
7
3171
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 thorughout the course the programs use so it will need to
8
10172
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". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
11
3790
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
5901
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 will prove useful. structure MyStruct { // this is a complicated struct declaration in the sense
45
18928
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 of their elements? Why can't I change the element itself? class Program { private struct MyStruct
1
2403
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 after execution...mean when i execute the program then intially program asked from user to Enter the value of data structure.
0
9826
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9674
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10860
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5659
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5831
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4466
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 we have to send another system
2
4026
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.