[comp.lang.c] Casting diff structures to char*

robert@nereid.jpl.nasa.gov (Robert Angelino) (05/13/91)

I've written a function that takes a char* pointer to different
structs that have a common named var (which I'm accessing in this
function).  I've included an example.

struct one
{
 int a,
     rnum;
};

struct two
{
 char b;
 int a,
     rnum;
};

my_func(rec,...)
	char	*is_this_correct;
		.
		.
		.
{
is_this_correct->rnum = ...

}

my_func((char*) &a,...);
my_func((char*) &b,...);


This is the error I'm getting
"nonunique name demands struct/union or struct/union pointer".

I keep getting this error in my_func where I try to access rec->rnum.
Am I doing this correctly??

Thanks in advance and any suggestions are welcome.

-- 
    -     ------       -                              Robert Angelino
   | |   | ----  \    | |                             ms T-1704L
   | |   | |   \ |    | |                             4800 Oak Grove Drive
   | |   | | --  |    | |                             Pasadena, CA 91109
---| |   | | \__/     | |___                          robert@triton.jpl.nasa.gov
\____|et |_|ropulsion |_____\aboratory                (818) 354-9574

willcr@bud.sos.ivy.isc.com (Will Crowder) (05/13/91)

In article <6214@mahendo.Jpl.Nasa.Gov>, robert@nereid.jpl.nasa.gov (Robert
Angelino) writes:

|> struct one
|> {
|>  int a,rnum;
|> };
|> struct two
|> {
|>  char b;
|>  int a,rnum;
|> };
|> my_func(rec,...)
|> 	char	*is_this_correct;
|> {
|> is_this_correct->rnum = ...
|> }
|> 
|> my_func((char*) &a,...);
|> my_func((char*) &b,...);

[gets compiler complaints about the is_this_correct->rnum]

|> Am I doing this correctly??

No.

The parameter you're passing to my_func() is a char *, which includes no
information about structure members.  The compiler has no idea what
is_this_correct->rnum means, and there is not enough information in the call
for it to know.

If you only need to access rnum and no other members of the structs within
the function, just declare it as:

my_func(rnump)
int *rnump;
(
     *rnump = ...
}

and call it as

my_func(&a.rnum);
my_func(&b.rnum);

If you need to access other members of structs a and b in the same function,
then you're going to have to include information about which kind of struct
pointer you're passing in the call, and the function itself is going to have
to have knowledge of those types.  Something like:

my_func(sp, type)
char *sp;
int type;
{
     if (type == STRUCTURE_TYPE_A)
          (struct a *)sp->rnum = ...
     else
          (struct b *)sp->rnum = ...
}

Pretty ugly, huh?

Also, if this is ANSI C, you want to use void * rather than char * as the
generic or "opaque" pointer.

Remember, don't pass more information than you need down to the function.

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