468,512 Members | 1,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Automatic struct initialization

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

Dec 16 '07 #1
2 3452
On Sun, 16 Dec 2007 05:09:50 +0000 (UTC),
dj******@csclub.uwaterloo.ca.invalid wrote in comp.lang.c:
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
Dec 16 '07 #2
On Sun, 16 Dec 2007 05:09:50 +0000 (UTC),
dj******@csclub.uwaterloo.ca.invalid wrote:
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
Dec 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by sathyashrayan | last post: by
10 posts views Thread by emma middlebrook | last post: by
6 posts views Thread by arnuld | last post: by
6 posts views Thread by Daniel Rudy | last post: by
10 posts views Thread by Juha Nieminen | last post: by
5 posts views Thread by ssylee | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.