By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,208 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

Local structs

P: n/a
Hi,

a question on local structs:

Usually, when a local variable is used without initialization,
it might hold any value. According to C99, does this rule
also apply to elements of local structs?
int main( void )
{
struct type {
int a;
int b;
int c;
} myStruct = { .b = 100 };
return 0;
}
Here, struct elements myStruct.a and myStruct.c are not
explicitely initialized. Do they contain an undefined value
or are they implicitely assign the value 0?

Cheers,
Chris
May 24 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 24/5/06 09:40, Christian Christmann wrote:
Hi,

a question on local structs:

Usually, when a local variable is used without initialization,
it might hold any value. According to C99, does this rule
also apply to elements of local structs?


My understanding is yes - if the struct has automatic storage duration,
then the values of uninitialised members is indeterminate. If the
struct has static duration, members are initialised to 0/NULL as
appropriate.
May 24 '06 #2

P: n/a
Christian Christmann wrote:
Hi,

a question on local structs:

Usually, when a local variable is used without initialization,
it might hold any value. According to C99, does this rule
also apply to elements of local structs?
int main( void )
{
struct type {
int a;
int b;
int c;
} myStruct = { .b = 100 };
return 0;
}
Here, struct elements myStruct.a and myStruct.c are not
explicitely initialized. Do they contain an undefined value
or are they implicitely assign the value 0?


If you initialise any part of a structure then the entire structure is
initialised. The parts you don't initialise are set to an appropriate 0
(including pointers being set to null).
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 24 '06 #3

P: n/a

Flash Gordon wrote:
Christian Christmann wrote:
Hi,

a question on local structs:

Usually, when a local variable is used without initialization,
it might hold any value. According to C99, does this rule
also apply to elements of local structs?

Is that the case with arrays or structs ???
Can you provide any authentic justification what you said. I dont agree
to it.
As the scope is local and the memory is allocted on the stack, will not
be initialised to zero.
Cheers,
Sandeepksinha.
int main( void )
{
struct type {
int a;
int b;
int c;
} myStruct = { .b = 100 };
return 0;
}
Here, struct elements myStruct.a and myStruct.c are not
explicitely initialized. Do they contain an undefined value
or are they implicitely assign the value 0?


If you initialise any part of a structure then the entire structure is
initialised. The parts you don't initialise are set to an appropriate 0
(including pointers being set to null).
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc


May 24 '06 #4

P: n/a
Christian Christmann posted:
Do they contain an undefined value or are they implicitely assign the
value 0?

int main()
{
double array1[50];

/* array1 contains white noise */
double array2[50] = {};

/* All of array2's elements are set to zero */
double array3[50] = { 1, 2, 3 };

/* array3's first three elements are set respectively to
1, 2, 3. All other elements are set to zero.

*/
struct Monkey { int a; char b; double c; };

Monkey monkey1;

/* monkey1 contains white noise */
Monkey monkey2 = {};

/* All of monkey2's elements are set to zero */
Monkey monkey3 = { 5 };

/* monkey3.i is set to 5. All other members are set to zero */

}

-Tomás
May 24 '06 #5

P: n/a
On 24 May 2006 04:00:52 -0700, "sandy" <sa***********@gmail.com>
wrote:

Flash Gordon wrote:
Christian Christmann wrote:
> Hi,
>
> a question on local structs:
>
> Usually, when a local variable is used without initialization,
> it might hold any value. According to C99, does this rule
> also apply to elements of local structs?
>
>

You left out the critical part of FG's message that says if part of an
struct is initialized explicitly, the rest of the object is
initialized implicitly.
Is that the case with arrays or structs ???
Both.
Can you provide any authentic justification what you said. I dont agree
From the 1184 draft of C99, section 6.7.8, paragraph 21

"If there are fewer initializers in a brace-enclosed list than there
are elements or members of an aggregate, or fewer characters in a
string literal used to initialize an array of known size than there
are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration."

This concept existed in C89 also. And probably before that.
to it.
Your agreement is not really required. We discuss the language as
defined by the standard, not your preferences. You can find the draft
using google and then have a reference that describes the way things
are as opposed to the way you think they are.
As the scope is local and the memory is allocted on the stack, will not
be initialised to zero.


C does not define a stack. You have no idea where the objects are
allocated.

C also does not define local scope. Even using the correct term,
block scope, doesn't help because scope does not determine if an
object is initialized implicitly, duration does. It is perfectly
reasonable to have a static object with block scope which will be
initialized implicitly if necessary.
Remove del for email
May 25 '06 #6

P: n/a
On Wed, 24 May 2006 12:12:39 GMT, "Tomás" <No.Email@Address> wrote:
Christian Christmann posted:
Do they contain an undefined value or are they implicitely assign the
value 0?

int main()
{
double array1[50];

/* array1 contains white noise */
double array2[50] = {};


I think you need at least one value.

/* All of array2's elements are set to zero */
double array3[50] = { 1, 2, 3 };

/* array3's first three elements are set respectively to
1, 2, 3. All other elements are set to zero.

*/
struct Monkey { int a; char b; double c; };

Monkey monkey1;

/* monkey1 contains white noise */
Monkey monkey2 = {};

/* All of monkey2's elements are set to zero */
Ditto.


Monkey monkey3 = { 5 };

/* monkey3.i is set to 5. All other members are set to zero */


monkey3 doesn't have a member i. I hope you meant a.
Remove del for email
May 25 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.