Local structs

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

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
 
G

Graham J Lee

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.
 
F

Flash Gordon

Christian said:
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).
 
S

sandy

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.
 
T

Tomás

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
 
B

Barry Schwarz

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.

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
 
B

Barry Schwarz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top