Pointers on pointers.

Will Crowder willcr at bud.sos.ivy.isc.com
Sat Apr 6 08:21:42 AEST 1991


In article <1991Apr5.173843.2435 at bnlux1.bnl.gov>, reilly at bnlux1.bnl.gov
(kevin reilly) writes:

|> I declare a structure that contains other structures:
|> struct strType {
|> struct dataType1 data1;
|> struct dataType2 data2;
|> };
|> 
|> If dataType1 is known in advance as containing only ints can I do the
|> following:
|> declaration: void FUNC1(int *input);
|> usage: FUNC1(&strType.data1);
|> 
|> as opposed to the more correct procedure:
|> declaration: void FUNC1(dataType *input);
|> usage: FUNC1(&strType.data1);
|> 
|> Is there something totally wrong with the first way?

Yes...It's totally wrong... :) :)  You're passing a pointer to one kind
of object to a function which expects a different kind of object.  This
is generally Not A Good Thing To Do.  Also, I'm always suspicious when
I use the phrase "known in advance" to justify a non-standard technique.
Chances are the things you "know in advance" will change at some point and
you'll have to track down all your tricks.

Actually, you'll get away with this kind of stuff with no problem on 90% of
the hardware one is likely to encounter.  Does that make it worthwhile?
Probably not.  It really depends on the real reason you're doing it this way.
(Count me as curious!)  Is there some problem with doing it the "correct" way?
Also, are you trying to access more than just the first int of the struct
with your FUNC1()?  If not, just pass "&strType.data1.int_member".  

|> I have been using it quitre successfully but it bothers me to go against
|> what I see in the standard texts.

It's good (IMHO) that this bothers you.  Although the phrase (and frequent
bumpersticker) "QUESTION AUTHORITY" comes to mind, in this case, the 
authorities are quite correct.  As I mentioned, you'll get away with this
on most hardware.  (Indeed, since I don't have access to/experience with
the more exotic architectures available, I can't readily come up with an
example where this doesn't work...Chris?? Doug?? Henry?? Karl??)  Of course,
any ANSI and many more modern compilers complain bitterly about this
kind of stuff.

|> Thank you very much.
|> Kevin M. Reilly
|> reilly at bnlux1.bnl.gov

Hope this helps,

Will

--------------------------------------------------------------------------------
Will Crowder, MTS            | "I was gratified to be able to answer quickly,
(willcr at ivy.isc.com)         |  and I did: I said I didn't know."
INTERACTIVE Systems Corp.    |		-- Mark Twain



More information about the Comp.lang.c mailing list