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
problem please?

Your help appreciated, and thanx in advance...

     __  _________________________________
    /  ||           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];

--
Wade Guthrie                     |

I don't speak for Rockwell.      |  Sucks Syntax.



Tue, 24 Nov 1998 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. structures within structures

2. Q about structures within structures

3. newbie Q on initialising structures

4. Problem declaring/initialising array of structures

5. newbie Q on initialising structures

6. initialising an empty structure?

7. Initialising Union members of Structures

8. Initialising structures

9. Initialising structure members - help please!

10. Initialising a structure - help please

11. Structue within a structure

12. how to dump easy-to-read structures from within C programs

 

 
Powered by phpBB® Forum Software