[comp.lang.c] Pointers on pointers.

reilly@bnlux1.bnl.gov (kevin reilly) (04/06/91)

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?
I have been using it quitre successfully but it bothers me to go against
what I see in the standard texts.

Thank you very much.
Kevin M. Reilly
reilly@bnlux1.bnl.gov

willcr@bud.sos.ivy.isc.com (Will Crowder) (04/06/91)

In article <1991Apr5.173843.2435@bnlux1.bnl.gov>, reilly@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@bnlux1.bnl.gov

Hope this helps,

Will

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

gwyn@smoke.brl.mil (Doug Gwyn) (04/06/91)

In article <1991Apr5.173843.2435@bnlux1.bnl.gov> reilly@bnlux1.bnl.gov (kevin reilly) writes:
>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);

If representation of pointer-to-int happens not to use the same scheme
as representation of pointer-to-structure, then using the incorrect
type would probably result in a malfunction.

You could also invoke the function with argument &strType.data1.x, where
"x" is the name of the first (int) member of the structure.

A more general comment is that there appears to be an imperfect
implementation of data abstraction underlying this example.