setting initial values in struct template

R

Romeo Colacitti

Section "1.2.5 Structures and Unions" of the C Library Reference Guide
(http://www.acm.uiuc.edu/webmonkeys/book/c_guide/1.2.html#struct)
explains that it is possible to initilize elements of all structs of a
certain type by providing an initializer when declaring the template of
the struct.

I tried it on my compiler and it's complaining. This is taken from the
C Library Reference Guide:

struct my-structure {
int fred[5];
char wilma, betty;
float barny=1;
};

This defines the structure my-structure, but nothing has yet been done.

struct my-structure account1;
This creates account1 and it has all of the variables from
my-structure. account1.barny contains the value "1".


I tried this on my system, and it complained about the = 1. I must say
if this feature was possible it would be very good because a lot of
structs have flags and it would be nice to initialize them all to 1 or
0.

Can someone tell me if this is possible? I really suck at searching
through the C89/99 drafts to find answers, and can't find this on
google. thank you
 
D

dot

Can someone tell me if this is possible? I really suck at searching
through the C89/99 drafts to find answers, and can't find this on
google. thank you

Check your compiler docs, see if it has a C99 compatibility flag. If it
does, turn it on.
 
K

Keith Thompson

Romeo Colacitti said:
Section "1.2.5 Structures and Unions" of the C Library Reference Guide
(http://www.acm.uiuc.edu/webmonkeys/book/c_guide/1.2.html#struct)
explains that it is possible to initilize elements of all structs of a
certain type by providing an initializer when declaring the template of
the struct.

I tried it on my compiler and it's complaining. This is taken from the
C Library Reference Guide:

struct my-structure {
int fred[5];
char wilma, betty;
float barny=1;
};

This defines the structure my-structure, but nothing has yet been done.

struct my-structure account1;
This creates account1 and it has all of the variables from
my-structure. account1.barny contains the value "1".

That web page is full of errors.

"my-structure" is not a valid identifier, something the author of the
page would have known if he'd taken the time to compile the code.

Neither C90 or C99 allows initializers on structure members. (Yes, it
would be a useful feature.)

Get yourself a copy of K&R2 (Kernighan & Ritchie, _The C Programming
Language_, 2nd edition).
 
K

Krishanu Debnath

Romeo said:
Section "1.2.5 Structures and Unions" of the C Library Reference Guide
(http://www.acm.uiuc.edu/webmonkeys/book/c_guide/1.2.html#struct)
explains that it is possible to initilize elements of all structs of a
certain type by providing an initializer when declaring the template of
the struct.

I tried it on my compiler and it's complaining. This is taken from the
C Library Reference Guide:

struct my-structure {
int fred[5];
char wilma, betty;
float barny=1;
};
[snip]

No, the last struct-declaration is 'syntatically' invalid. You may look
for C++
implementation where through constructor you can initialize the
structure's fields.

Also looking at structure's tag name , I have the feeling you should
read a better
c material.

Krishanu
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top