Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!wuarchive!mit-eddie!snorkelwacker!paperboy!meissner From: meissner@osf.org (Michael Meissner) Newsgroups: comp.lang.c Subject: Re: Cast struct * --> double * --> struct * Portable ? Message-ID: Date: 12 Oct 90 16:23:23 GMT References: <588@dptechno.UUCP> Sender: news@OSF.ORG Organization: Open Software Foundation Lines: 44 In-reply-to: dave@dptechno.uucp's message of 11 Oct 90 22:20:16 GMT In article <588@dptechno.UUCP> dave@dptechno.uucp (Dave Lee) writes: | 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 ? No, you must have at least one double member within the structure or union to assure it gets the appropriate alignment. For passing generic pointers, consider void * in ANSI systems with a fallback to char * on moldy old, uncared for systems. In fact if you want the strongest alignment requirement within ANSI, it must have a member whose type is `long double'. All this aside, it seems a rather dubious programming practice. I would recomend, just declaring the pointer to be struct *, and just not declare within the function. This is legal, and even encouraged. It also should work on most real-world non-ANSI C compilers. -- Michael Meissner email: meissner@osf.org phone: 617-621-8861 Open Software Foundation, 11 Cambridge Center, Cambridge, MA, 02142 Do apple growers tell their kids money doesn't grow on bushes?