Structure and Array equivalence

Dave Lee dave at dptechno.uucp
Sat Sep 15 10:20:43 AEST 1990


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



More information about the Comp.lang.c mailing list