Cast struct * --> double * --> struct * Portable ?

Dave Lee dave at dptechno.uucp
Fri Oct 12 08:20:16 AEST 1990


Assuming the following :

struct  somestruct  {
	/*** data */
} ;


f_s( s )
struct somestruct *s;
{
	/* do something with s */
}

f_d( d , special )
double *d;
int special;
{

	if( special ) 
		f_s( (struct somestruct *) d );
	else 
		/* Something with a double * */
}	

Will the following call be generally portable ?

struct somestruct THIS_ONE;
	f_d( (double *) &THIS_ONE , 1 );


Restated:  is the following series of casts portable ?

	struct * -->  double * --> struct *  

Does this require that the structure be alligned on a sizeof(double) boundry ?
Would placing a double as the first member of the structure guarentee such
allignment ?   I Believe that if the structure were allocated with malloc()
that the resulting pointer would be worst case alligned and therefore
will not suffer from allignment problems.  How about static or auto structs ?

I understand that this is not good programming, but there exists a large number
of routines in a program that are already coded to expect a double * and I would
prefer not to have to duplicate all that code just to add this special case.

I suspect that this is not truely portable, but are there any "likely" problems
with this?  Is there some kludge I should do to make this work, like placing
a double as the first member of somestruct ?

Please Email or Post as convient. 

-- 
Dave Lee
uunet!dptechno!dave



More information about the Comp.lang.c mailing list