Automatic struct initialization

D

dj3vande

Is this code, as the complete contents of a translation unit, valid C90
that initializes the struct foo to contain copies of the values passed
to bar()?
--------
struct foo
{
int i;
void *v;
};

int bar(int i,void *v)
{
struct foo f={i,v}; /*line 9*/
return f.i;
}
--------

GCC accepts it with -ansi, but complains about it with -ansi -pedantic:
--------
dj3vande@buttons:~ (0) $ gcc -ansi -pedantic -c foo.c
foo.c: In function 'bar':
foo.c:9: warning: initializer element is not computable at load time
foo.c:9: warning: initializer element is not computable at load time
dj3vande@buttons:~ (0) $ gcc -ansi -c foo.c
dj3vande@buttons:~ (0) $
--------

I can't find anything in N1124 that disallows it, though (I'm looking
at section 6.7.8, "Initialization"), and don't have a copy of C90 to
see whether it's changed since then.


dave
 
J

Jack Klein

On Sun, 16 Dec 2007 05:09:50 +0000 (UTC),
Is this code, as the complete contents of a translation unit, valid C90
that initializes the struct foo to contain copies of the values passed
to bar()?
--------
struct foo
{
int i;
void *v;
};

int bar(int i,void *v)
{
struct foo f={i,v}; /*line 9*/
return f.i;
}
--------

No, it's not, although it is a very common extension. It is a
constraint violation under C90, listed in the constraints section of
6.5.7 Initialization, which says:

"All the expressions in an initializer for an object that has static
storage duration or in an initializer list for an object that has
aggregate or union type shall be constant expressions."

And, of course, a struct is an aggregate type and the parameters
passed to a function are certainly not constant expressions.
GCC accepts it with -ansi, but complains about it with -ansi -pedantic:
--------
dj3vande@buttons:~ (0) $ gcc -ansi -pedantic -c foo.c
foo.c: In function 'bar':
foo.c:9: warning: initializer element is not computable at load time
foo.c:9: warning: initializer element is not computable at load time
dj3vande@buttons:~ (0) $ gcc -ansi -c foo.c
dj3vande@buttons:~ (0) $
--------

I can't find anything in N1124 that disallows it, though (I'm looking
at section 6.7.8, "Initialization"), and don't have a copy of C90 to
see whether it's changed since then.

The code is legal under C99. The relevant paragraph is 6.7.8 P4 (in
the constraints section) which says:

"All the expressions in an initializer for an object that has static
storage duration shall be constant expressions or string literals."

....dropping the wording about aggregate or union type.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
D

David Thompson

On Sun, 16 Dec 2007 05:09:50 +0000 (UTC),
int bar(int i,void *v)
{
struct foo f={i,v}; /*line 9*/
GCC accepts it with -ansi, but complains about it with -ansi -pedantic:
I can't find anything in N1124 that disallows it, though (I'm looking
at section 6.7.8, "Initialization"), and don't have a copy of C90 to
see whether it's changed since then.
As Jack Klein said, it was indeed changed from C89/90. You can see it
listed, though not detailed, in the list in the Foreword on page xiii:
"relaxed constraints on aggregate and union initialization"
and it is spelled out in 6.7.8 in C99RationaleV5.10.pdf
which also is beer-free (gratis) on the WG14 site last I looked.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top