473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

GCC structure initialization

Hello,

I have this:

struct mystruct {
int a;
union {
int b;
int c;
};
};

When I initialize it:

struct mystruct implement = {
.a = 1,
.b = 2
};

I get the following error from gcc:

error: unknown field `b' specified in initializer

Is it possible to do this kind of initialization alltogether with gcc?
How can I initialize the anonymous union in the struct listed above?

I know this is a gcc feature, but I wondered if there anyone here that
might know this, please.
Thanks,
Svein
Nov 14 '05 #1
2 7265
"Svein E. Seldal" <Sv**************@solidas.com> wrote in message
news:7Y******************@news2.e.nsc.no...
[snip]
How can I initialize the anonymous union in the struct listed above?


By making it not anonymous? Questions regarding GCC-specific features should
be asked in a GCC group. Although IIRC, C99 has something similar (or
identical).

Alex
Nov 14 '05 #2
Svein E. Seldal wrote:
Hello,

I have this:

struct mystruct {
int a;
union {
int b;
int c;
};
};

When I initialize it:

struct mystruct implement = {
.a = 1,
.b = 2
};

I get the following error from gcc:

error: unknown field `b' specified in initializer

Is it possible to do this kind of initialization alltogether with gcc?
How can I initialize the anonymous union in the struct listed above?

I know this is a gcc feature, but I wondered if there anyone here that
might know this, please.


Anonymous unions are off-topic here. A gnu* newsgroup would do better.
Obviously, naming the union would work here.

<ot>
In your case either
struct mystruct implement = {
.a = 1, {2}
};
or
struct mystruct implement = {
.a = 1
};
implement.b = 2;
should work.
</ot>
Nov 14 '05 #3

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

Similar topics

2
by: Kevin P. Fleming | last post by:
I've got an enum and a structure: enum option_type { O_STRING, O_BOOL, O_NUMBER }; struct option_s { char *name;
9
by: Skybuck Flying | last post by:
Hello, What does Const mean in this c structure ? and what is the delphi equivalent ? I think const struct just means it can't be modified... is that correct ? Struct { Type1 Field1;...
2
by: kimimaro | last post by:
hi I wonder if array can be work along with structure? Below are the declaration of my structure struct employee{ char ID; char Name; char Department;
14
by: Stef | last post by:
Hello, I have a question: Is it possible to init a 2d array of structures to zero ? For example with array I do. int array = {0} but:
5
by: aarklon | last post by:
Hi all, why the following structure initialization is not valid #include<stdio.h> struct rec { char name; int age; };
11
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
4
by: koffee | last post by:
Hi people, I've got a question on global structure initialization. A global variable like int can be explicitly initialized as: int Global = 1; But how can I explicitly initialize a...
17
by: jb.simon | last post by:
Recently I was pinged in a code review about my use of the initialization method AStruct myStruct = { 0 } ; Which initializes all elements of the myStruct to 0. I was questioned on it because...
14
by: Bill Reid | last post by:
OK, let's say that have a function menu structure declaration like this (since I basically do): typedef struct function_item { char *descrip; void(*function)(void); } t_function_item; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.