Initialising Structure Within Structure...How?!
Author Message
Initialising Structure Within Structure...How?!

Hi,
As the title says, how can you initialise structure within another
structure without defining the size of the second structure (inner)?

For example, consider the following code:

/* Example Of Initialising Structure Inside Structure */
#include <stdio.h>

struct Location {
struct Position {
int     X;
int     Y;
} Point[2];  /* If you don't define the size, you'll get an error */
int Z;

Quote:
};

/* Here Iam trying to initialise the structure (POS) */
struct Location Pos = { {{2,5}, {10,90}} , 0};

void main()
{
Pos.Z = Pos.Point[0].X + Pos.Point[0].Y;
printf("\nZ = %d\n", Pos.Z);
Pos.Z = Pos.Point[1].X + Pos.Point[1].Y;
printf("Z = %d\n", Pos.Z);

Quote:
}

This programme will run as long as you define the size of the structure
of (POSITION) which called (POINT). But if you don't define the size,
which I would (strongly) like to do here, you'll get this error:

Error...: Size of the type is unknown or zero

Do you know what's wrong with my code and how can I get around this

__  _________________________________
/  ||           Hashem Al-Dhaheri

/____||      ____     ___     ___ ___
/     ||  -  /   //__//__//__//__ /___) /
/      ||___ /___//  //  //  //__ /   \ /

Sat, 14 Nov 1998 03:00:00 GMT
Initialising Structure Within Structure...How?!

: struct Location {
:    struct Position {
:         int     X;
:         int     Y;
:    } Point[2];  /* If you don't define the size, you'll get an error */
:    int Z;
: };

This restriction is inherent to the language: given a structure's
declaration its layout is constant. To allow variable sized array,
use a pointer:
struct Location{
struct Position *Point;
int Z;
};
and then allocate the point field separately:
struct Location zoe;
zoe.Point = malloc(jamie*sizeof(struct Point));
zoe.Z = victoria;

There are other ways, but this should work for now.
--

the Queen who straits, the Queen of strife;|          Cupertino, California
with gasp of death or gift of breath       | (xxx)xxx-xxxx            95015
she brings the choice of birth or knife.   |         I don't use no smileys

Sun, 15 Nov 1998 03:00:00 GMT
Initialising Structure Within Structure...How?!

Quote:

>   As the title says, how can you initialise structure within another
>structure without defining the size of the second structure (inner)?

You can't.  How can you expect the compiler to operate correctly
when it doesn't know the size of its objects?

Quote:
> For example, consider the following code:
>struct Location {
>   struct Position {
>        int     X;
>        int     Y;
>   } Point[2];  /* If you don't define the size, you'll get an error */
>   int Z;
>};

Now, on the other hand, if you want an arbitrary number of points
you can always dynamically allocate.

struct Position {
int     X;
int     Y;
};

struct Location {
Location() Z(0), point(0) {};
struct Position *point;
int             z;
} location;

location.point = new Position[2];

--