473,796 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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@button s:~ (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@button s:~ (0) $ gcc -ansi -c foo.c
dj3vande@button s:~ (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 3895
On Sun, 16 Dec 2007 05:09:50 +0000 (UTC),
dj******@csclub .uwaterloo.ca.i nvalid 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@button s:~ (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@button s:~ (0) $ gcc -ansi -c foo.c
dj3vande@button s:~ (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.l earn.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.i nvalid 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.ne t
Dec 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7662
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
10
8649
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. That struck me as a bit odd really as I would have thought it would have only bothered about 'tracking' an 'atomic'/'complete' (via 'new()') initialization not doing it 'bit by it'!!! Any comments? Seems a bit of an overhead tracking memberwise...
3
1919
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
5069
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 deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
6
3044
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 table of the name sof months o fyear and the number of days in each month. write out that table. Do this twice:
18
9127
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: struct st_a { int i, j; };
6
15113
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 = { {0x00, 0x00, 0x00},
10
5072
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
11675
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
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.