Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

initializing structures.

Status
Not open for further replies.

denc4

Programmer
Feb 17, 2005
107
0
0
NL
consider the following type definitions:
Code:
  struct STRUCTURE0
  {
    int integer0;
  };

  struct STRUCTURE1
  {
    int integer1;

    struct STRUCTURE2
    {
      int integer2;
    } struct0;

    struct STRUCTURE0 struct1;
  } struct2;
when i initialize struct0 and struct1 with some value, lets say:
Code:
  ...
  } struct0 = {0};

and

  struct STRUCTURE0 struct1 = {1};
my compiler (microsoft C 7) won't accept it, it does allow me to do a full or partial intitialization of struct2:
Code:
  ...
  } struct2 = {0,1,2};

or

  ...
  } struct2 = {0,1};
Is not being able to initialize struct0 and struct1 normal behaviour under ANSI-C?
i think my compiler more likely proves its normal, but i would like a second opinion.
thx.
 
Well struct0 and struct1 are not separate variables, they're just members of struct0.

To initialise struct2, it would be
Code:
struct STRUCTURE1
{
  int integer1;
  struct STRUCTURE2
  {
    int integer2;
  } struct0;
  struct STRUCTURE0 struct1;
} struct2 = {
  0,      /* .integer1 */
  { 1 },  /* .struct0.integer2 */
  { 2 }   /* .struct1.integer0 */
};
Each nested struct, union or array inside a struct needs it's own pair of braces.

If you want to create separate variables of the nested structs, then do something like this.
Code:
struct STRUCTURE2 foo = { 1 };
struct STRUCTURE0 bar = { 2 };

Then because C supports structure assignment, you can do this
Code:
struct2.struct0 = foo;
struct2.struct1 = bar;

PS.
Without checking, I'm not sure that STRUCTURE2 has global scope.

--
 
i tried the extra braces, and compared the assembly listing with the one without the extra braces. they turned out the same so it seems to have the same effect.

STRUCTURE2 does have global scope, i can create an instance of it outside STRUCTURE1..

the structure assigment is a possibility, but right now i'm just checking out the do's and don'ts of structure
definitions and their initialization values.

so is it safe to say that i cannot initialize nested structures in ANSI-C?
 
> so is it safe to say that i cannot initialize nested structures in ANSI-C?
Only by the same rules which apply to every other aggregate, and that is that you must specify all the prior members.
C will fill the remainder with zeros of the appropriate type.

So in this case, to initialise upto and including struct0, it would be
Code:
} struct2 = {
  0,      /* .integer1 */
  { 1 }   /* .struct0.integer2 */
};

--
 
what do you mean by "that you must specify all the prior members".. i can only do that at struct2 when initializing.
not at struct0, and struct1..
thx.
 
> what do you mean by "that you must specify all the prior members".
It means everything before the one you really want to initialise, though I don't know why you would only want to initialise part of a structure.

With assignment, you can directly access any part of the struct as you see fit.

Initialisation is just an ordered list of constants starting at the beginning of the structure and working its way member by member to the end of the structure, consuming initialisation values as it goes.


--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top