Order of allocation of fields in a struct?

Charles Lakos charles at tasis.eecs.utas.edu.au
Thu Jun 14 00:00:00 AEST 1990


I wonder if the C standard says anything about the order of allocation of 
memory to fields of a struct.  In particular, suppose that I have
  typedef struct {
    int a;  char c;  real b;
  } t1;
  typedef struct {
    int a;  char c;  real b;  something-else d;
  } t2;
  t2 v2, *p
  ...
  p = &v2;  /* set p to point to v2 of type t2 */
Can I then access the fields of v2 in the following way:
  (t1 *) p -> b

If this is not standardised, is it common practice?

(The question interests me from the point of view of implementing object-
oriented languages in C.  Classes could be implemented as struct's and
pointers to the object could be coerced to the relevant type without
problem.)




-----
Charles Lakos                                   charles at tasis.eecs.utas.edu.au
Electrical Engineering & Computer Science
University of Tasmania
Tasmania, Australia.



More information about the Comp.std.c mailing list