What is correct Ansi way of passing pointers to structures

Larry Jones scjones at sdrc.UUCP
Mon Jun 11 01:52:53 AEST 1990


In article <7438 at ucdavis.ucdavis.edu>, kuan at iris.ucdavis.edu (Frank [Who me?] Kuan) writes:
> With Aztec C Compiler, there's an option to automatically generate
> prototypes, but it gets confused with typedefs. For Example:
> 
> typedef struct {
> 	int x,y;
> } funstuff; 
> 
> 
> void dofunstuff(funstuff *p)
> {
> ...
> }
> 
> The prototype generated by my compiler looks something like:
> 
> void dofunstuff(struct *funstuff)
> 
> Does anyone know why this is? 

Most automatic prototype generators expand typedefs into their
underlying C types.  That way the prototype can stand alone
without having to have the typedef in scope, but it does cause a
number of problems.  First is that some typedefs are strictly
necessary for portablility reasons and really shouldn't be
expanded (I don't really want my FILE * to turn into struct _iob
*) and, secondly, some typedefs don't HAVE a valid expansion
(such as your anonymous struct type).

> right now, what I've done to fix it is to declare
> it like this:
> 
> void dofunstuff(void *p)
> 
> and then cast it to point to what I want to within the routine.
> This is cumbersome and unelegant. What is the proper way to do
> things?

Well, I certainly wouldn't recommend that!  What you should do is
use a tag for the structure so that the generated prototype is
usable:

	typedef struct funstuff {
		int x,y;
	} funstuff; 

(note that there is no problem using the same name since struct
tags and typedefs are in different name spaces).

Another good idea is to generate prototypes as infrequently as
possible and then modify the generated prototypes to use the
typedefs you want and maintain them by hand from then on.
----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones at SDRC.UU.NET
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
"You know how Einstein got bad grades as a kid?  Well MINE are even WORSE!"
-Calvin



More information about the Comp.lang.c mailing list