473,388 Members | 944 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,388 software developers and data experts.

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 3870
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
10
by: emma middlebrook | last post by:
Hi I discovered that if you declare a structure (and not 'new()' it) you can then separately initialize its members and the compiler counts those separate statements as a full initialization....
3
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
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...
6
by: arnuld | last post by:
this one was much easier and works fine. as usual, i put code here for any further comments/views/advice: --------- PROGRAMME ------------ /* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a...
18
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: ...
6
by: Daniel Rudy | last post by:
Hello Group. Please consider the following code: /* this table is used in the wipedevice routine */ static const struct wipe_t { uchar wte; /* wipe table entry */ } wipetable = {...
10
by: Juha Nieminen | last post by:
I suppose you can never know C++ fully. I would have never guessed this actually compiles and works: struct Point { int x, y; }; struct Line { Point endpoint; int weight; };
5
by: ssylee | last post by:
I'm not sure if I can initialize members of a struct the lazy way. For example, let's say I have a struct defined as below: typedef struct _ABC { BOOL A; BOOL B; BOOL C; } ABC;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
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...

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.