How is a structure stored in memory?

Miguel Angel miguel.nunez@terra.es
Tue Apr 6 08:58:00 GMT 2004


Hello experts,

  I have a problem ;-) I have a structure, its first field being a 8 bits
  type.

    struct {
      U8  firstfield;
      .
      .
      .
    } st;

    char* p = &st;

  If I access the structure through a char pointer, am I accesing the
  first 8 bits field?? The first field will always be stored at the
  first byte (the one pointed by p)? This depends on the compiler?

  The origin of the problem: I have a function with an only one
  argument, but I have to pass several kind of types through that
  argument. I first thougth the argument to be a union containing all
  the possible types. But it is really a lot of types.

  I try the argument to be a char array. I will pass a first byte
  being a kind of index to identify the type. Following the first byte
  it is the type byte per byte. Inside the function I can examine the
  first byte and decide to do a cast, according to the correct type,
  to format the rest of bytes.

Thanks a lot,
Miguel Angel



More information about the Gcc-help mailing list