Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Automatic struct initialization

Reply
Thread Tools

Automatic struct initialization

 
 
dj3vande@csclub.uwaterloo.ca.invalid
Guest
Posts: n/a
 
      12-16-2007
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

 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      12-16-2007
On Sun, 16 Dec 2007 05:09:50 +0000 (UTC),
lid 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
 
Reply With Quote
 
 
 
 
David Thompson
Guest
Posts: n/a
 
      12-30-2007
On Sun, 16 Dec 2007 05:09:50 +0000 (UTC),
lid 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
static struct initialization in a Class:: -- not my struct christian.bongiorno@gmail.com C++ 2 09-20-2006 06:53 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57