dave@dptechno.uucp (Dave Lee) (09/15/90)
How portable is the following: Assume TYPE is any basic type (char,short,int,double,float ...) Assume N is a constant > 0 struct A_STRUCT { TYPE T1,T2,T3,T4,T5 .... TN; } A ; TYPE B[N]; TYPE C; funcA( X ) A_STRUCT *X; { X->T3 = C; } funcB( X ) TYPE *X; { X[2] = C ; } main() { funcA( &A ); funcB( (TYPE *) &A ); /**** OR ******/ funcA( (A_STRUCT) B ); funcB( B ); } ------- Q: Are funcA() and funcB() GUARENTEED to do the same thing ? Restated: If you have a structure which contains members of only one type, and you have an array of the same type with the same number of elements, is the compiler guarenteed to place the members in the same offsets in both the array and structure ? The particular case I am interested is with double, and int, but the general question is the same. Does the standard differ with K&R and ANSI ? All implementations I have ever seen work this way, but is this just luck ? EMail or Post as convient. Thank you . -- Dave Lee uunet!dptechno!dave
kaiser@ananke.stgt.sub.org (Andreas Kaiser) (09/16/90)
In a message of <Sep 15 00:20>, Dave Lee (dave@dptechno.uucp ) writes:
DL> struct A_STRUCT {
DL> TYPE T1,T2,T3,T4,T5 .... TN;
DL> } A ;
DL> funcA( X )
DL> A_STRUCT *X;
DL> {
DL> X->T3 = C;
DL> }
DL> funcB( X )
DL> TYPE *X;
DL> {
DL> X[2] = C ;
DL> }
DL> If you have a structure which contains members of only one type,
DL> and you have an array of the same type with the same number of elements,
DL> is the compiler guarenteed to place the members in the same offsets in
DL> both the array and structure ?
There is no guarantee that the any alignment padding is the same within
structures and arrays. There is no guarantee for any kind of alignment anyway.
DL> Q: Are funcA() and funcB() GUARENTEED to do the same thing ?
No.
Gruss, Andreas
--
:::::::::::::::::::: Internet: kaiser@ananke.stgt.sub.org
:: Andreas Kaiser :: Fidonet: 2:507/18.7206 (+ 2:509/5.2512)
::::::::::::::::::::