Re: typedef and declaration of function

From: xarax (xarax_at_email.com)
Date: 01/14/04


Date: Wed, 14 Jan 2004 21:15:07 GMT


"Vu Pham" <vu@sivell.com> wrote in message
news:bu4ajo$dknat$1@ID-219297.news.uni-berlin.de...
>
> "Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message
> news:bu49ok$dqi$1@oravannahka.helsinki.fi...
> > Vu Pham <vu@sivell.com> scribbled the following
> > on comp.lang.c:
> > > I think this problem relates to either c or c++ ( but I am not sure
> which
> > > one ) so I post to both of these news group. I am sorry if I did
> something
> > > wrong here.
> >
> > > Is there any difference between these two declarations :
> >
> > > 1.
> > > void * functionA( char * p, int s, int * e );
> >
> > > and
> > > 2.
> > > typedef void * ( *functionA_t)( char * p, int s, int * e );
> > > functionA_t functionA();
> >
> > Yes, there is. The first declares a function taking three arguments,
> > of types (char *), (int) and (int *), and returning a (void *).
> > The second declares a function taking an unspecified number of
> > arguments, and returning a pointer to a function that is declared as
> > in the first case.
> > That's a pretty fundamental difference.
>
> Thanks for the explanation.
>
> Shame one me :-(
>
> My problem is :
> I need to use the declaration of typedef as in 2, and I also need to
> declare the functionA like declared in 1, but with the definition of
> functionA_t so that whenever I change functionA_t functionA declaration
> will be changed correspondingly.
>
> How do I do that ?

No can do directly, because function declarations must
be specific (for a lot of reasons, including compatibility
between separately compiled source files).

Having said that, you could try something like this. Define
a typedef name for the result type. Then define a typedef
name for structure type that will hold the parameters for
the function. Then declare the function to accept a single
parameter that is a pointer to the structure typedef name
and returns the typedef for the result type.

===============================================
/* Declare the parameters */
typedef struct _func_a_parms_
{
    char * p;
    int s;
    int * e;
} FuncAParms;

/* Declare the result type. */
typedef void * FuncAResult;

/* Now declare the function. */
FuncAResult functionA(FuncAParms * funcAParmsP);
===============================================

When you want to change the parameters or the
result type of functionA, just change the typedef's
and recompile everything that calls functionA(). Callers
will have to pass a pointer to a FuncAParms structure
and they are responsible for properly initializing all
of the fields. The functionA() can pull the parameters
from the structure via the funcAParmsP pointer parameter.

Put the function declaration and the typedefs into a
header file. Put the function definition in a source file.
Every caller must #include the header file so the latest
version of the typedef's are available to all callers. Use
an automated version control facility (like MAKE or Ant)
to recompile everything that depends on the header file
when it changes.

You will also have to inspect individually each caller
to be sure that a change to the parameter structure is
compatible with the caller's initialization of that structure.

I think you are asking for a maintenance headache for
this kind of flexibility in a function declaration.

-- 
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Are ISV upgrade fees too high? Check our custom product development!


Relevant Pages

  • Re: External structs - newbie question
    ... The header file for csd.c contains the typedef for a ... There is only one struct in use ... Declaring it in a header file is the right way to go. ... If you put the keyword extern before the declaration, it will not be a problem declaring it twice. ...
    (comp.lang.c)
  • Re: What this mean? Compiler output
    ... This probably means that you have multiple declaration of the ... And then a C source file includes both, the typedef for ccc will be ... any single header file more than once, ...
    (comp.lang.c)
  • Re: External structs - newbie question
    ... The header file for csd.c contains the typedef for a ... by the declaration of the global variable map. ... Put an extern declaration of map in the header file, ...
    (comp.lang.c)
  • Re: incorrect warning?
    ... before the typedef for show, ... typedef void (struct thingTag *); ... Since there's been no complete declaration of that type, ... such an incomplete type would be completed later. ...
    (comp.lang.c)
  • Re: typedef and declaration of function
    ... > I need to use the declaration of typedef as in 2, ... a typedef name for the result type. ... Every caller must #include the header file so the latest ... version of the typedef's are available to all callers. ...
    (comp.lang.c)