Connecting Tech Pros Worldwide Forums | Help | Site Map

typedef and declaration of function

Vu Pham
Guest
 
Posts: n/a
#1: Jul 22 '05
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();

I thought they are the same, but I must be wrong somewhere. The function is
compiled and linked into a .so file. For the first declarartion, nm(1)
shows that name in the .so file, but not for the 2nd declaration.

Each of these declaration is embraced by extern "C" { }. The implenetation
file is a .cpp compiled with g++.

Thanks,

Vu



Joona I Palaste
Guest
 
Posts: n/a
#2: Jul 22 '05

re: typedef and declaration of function


Vu Pham <vu@sivell.com> scribbled the following
on comp.lang.c:[color=blue]
> 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.[/color]
[color=blue]
> Is there any difference between these two declarations :[/color]
[color=blue]
> 1.
> void * functionA( char * p, int s, int * e );[/color]
[color=blue]
> and
> 2.
> typedef void * ( *functionA_t)( char * p, int s, int * e );
> functionA_t functionA();[/color]

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.
[color=blue]
> I thought they are the same, but I must be wrong somewhere. The function is
> compiled and linked into a .so file. For the first declarartion, nm(1)
> shows that name in the .so file, but not for the 2nd declaration.[/color]

They are not the same as all. Maybe that's why you are getting
different results.
Note that .so files and nm(1) are off-topic here.
[color=blue]
> Each of these declaration is embraced by extern "C" { }. The implenetation
> file is a .cpp compiled with g++.[/color]

This smacks of C++, which is also off-topic here. But I can say that
the same I said above applies for C++, with one difference: replace
"an unspecified number of arguments" with "no arguments".

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'I' is the most beautiful word in the world."
- John Nordberg
Joona I Palaste
Guest
 
Posts: n/a
#3: Jul 22 '05

re: typedef and declaration of function


Joona I Palaste <palaste@cc.helsinki.fi> scribbled the following
on comp.lang.c:[color=blue]
> Vu Pham <vu@sivell.com> scribbled the following
> on comp.lang.c:[color=green]
>> Is there any difference between these two declarations :[/color][/color]
[color=blue][color=green]
>> 1.
>> void * functionA( char * p, int s, int * e );[/color][/color]
[color=blue][color=green]
>> and
>> 2.
>> typedef void * ( *functionA_t)( char * p, int s, int * e );
>> functionA_t functionA();[/color][/color]
[color=blue][color=green]
>> I thought they are the same, but I must be wrong somewhere. The function is
>> compiled and linked into a .so file. For the first declarartion, nm(1)
>> shows that name in the .so file, but not for the 2nd declaration.[/color][/color]
[color=blue]
> They are not the same as all. Maybe that's why you are getting
> different results.
> Note that .so files and nm(1) are off-topic here.[/color]

I meant "not the same at all". Sorry for the confusing typo.

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"C++. C++ run. Run, ++, run."
- JIPsoft
red floyd
Guest
 
Posts: n/a
#4: Jul 22 '05

re: typedef and declaration of function


Vu Pham wrote:[color=blue]
> 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();
>
> I thought they are the same, but I must be wrong somewhere. The function is
> compiled and linked into a .so file. For the first declarartion, nm(1)
> shows that name in the .so file, but not for the 2nd declaration.
>
> Each of these declaration is embraced by extern "C" { }. The implenetation
> file is a .cpp compiled with g++.
>
> Thanks,
>
> Vu
>[/color]

It's the same as the difference between:

extern int a;
and
int *a;

The first one is a declaration of an integer.
The second one defines a variable of type pointer to int.

In your first example, functionA the declaration of a function
returning void*, taking char *, int, and int*

In your second example functionA is a POINTER TO A FUNCTION returning
void*, taking char *, int, and int*

Does that make some sense?

Joona I Palaste
Guest
 
Posts: n/a
#5: Jul 22 '05

re: typedef and declaration of function


red floyd <no.spam@here.dude> scribbled the following
on comp.lang.c:[color=blue]
> Vu Pham wrote:[color=green]
>> 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();
>>
>> I thought they are the same, but I must be wrong somewhere. The function is
>> compiled and linked into a .so file. For the first declarartion, nm(1)
>> shows that name in the .so file, but not for the 2nd declaration.
>>
>> Each of these declaration is embraced by extern "C" { }. The implenetation
>> file is a .cpp compiled with g++.[/color][/color]
[color=blue]
> It's the same as the difference between:[/color]
[color=blue]
> extern int a;
> and
> int *a;[/color]

No, it isn't.
[color=blue]
> The first one is a declaration of an integer.
> The second one defines a variable of type pointer to int.[/color]
[color=blue]
> In your first example, functionA the declaration of a function
> returning void*, taking char *, int, and int*[/color]
[color=blue]
> In your second example functionA is a POINTER TO A FUNCTION returning
> void*, taking char *, int, and int*[/color]

Note the () after the symbol functionA. This makes functionA
*A FUNCTION* returning the pointer to a function you defined above.
[color=blue]
> Does that make some sense?[/color]

Pretty much... =)

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Normal is what everyone else is, and you're not."
- Dr. Tolian Soran
Ron Natalie
Guest
 
Posts: n/a
#6: Jul 22 '05

re: typedef and declaration of function



"Vu Pham" <vu@sivell.com> wrote in message news:bu49bv$dddep$1@ID-219297.news.uni-berlin.de...[color=blue]
>
> 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();[/color]

Yes, the second defines a function called functionA that returns a pointer
to function. functionA_t is type pointer to function.

You could
typedef void (functionA_t) (char*, int, int*);
functionA_t functionA; // note no parens.

Vu Pham
Guest
 
Posts: n/a
#7: Jul 22 '05

re: typedef and declaration of function



"Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message
news:bu49ok$dqi$1@oravannahka.helsinki.fi...[color=blue]
> Vu Pham <vu@sivell.com> scribbled the following
> on comp.lang.c:[color=green]
> > I think this problem relates to either c or c++ ( but I am not sure[/color][/color]
which[color=blue][color=green]
> > one ) so I post to both of these news group. I am sorry if I did[/color][/color]
something[color=blue][color=green]
> > wrong here.[/color]
>[color=green]
> > Is there any difference between these two declarations :[/color]
>[color=green]
> > 1.
> > void * functionA( char * p, int s, int * e );[/color]
>[color=green]
> > and
> > 2.
> > typedef void * ( *functionA_t)( char * p, int s, int * e );
> > functionA_t functionA();[/color]
>
> 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.[/color]

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 ?

Thanks,

Vu


Ron Natalie
Guest
 
Posts: n/a
#8: Jul 22 '05

re: typedef and declaration of function


[color=blue]
> In your second example functionA is a POINTER TO A FUNCTION returning
> void*, taking char *, int, and int*[/color]

Nope, actually it's a function returning the above pointer-to-function.


Ron Natalie
Guest
 
Posts: n/a
#9: Jul 22 '05

re: typedef and declaration of function



"Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message news:bu49ok$dqi$1@oravannahka.helsinki.fi...[color=blue]
> 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.[/color]

Actually it's a function taking NO arguments (not an unspecified number
of arguments) in C++. The original poster needs to figure out what language
he is programming in.

Vu Pham
Guest
 
Posts: n/a
#10: Jul 22 '05

re: typedef and declaration of function



"Ron Natalie" <ron@sensor.com> wrote in message
news:400566ab$0$71362$9a6e19ea@news.newshosting.co m...[color=blue]
>
> "Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message[/color]
news:bu49ok$dqi$1@oravannahka.helsinki.fi...[color=blue][color=green]
> > 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.[/color]
>
> Actually it's a function taking NO arguments (not an unspecified number
> of arguments) in C++. The original poster needs to figure out what[/color]
language[color=blue]
> he is programming in.
>[/color]

I use c++ for the implementation file, but that function needs to be
exported ( from an.so ) and I use extern "C" { } in the declaration file (
..h ) to prevent the name mangling.

Vu


xarax
Guest
 
Posts: n/a
#11: Jul 22 '05

re: typedef and declaration of function


"Vu Pham" <vu@sivell.com> wrote in message
news:bu4ajo$dknat$1@ID-219297.news.uni-berlin.de...[color=blue]
>
> "Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message
> news:bu49ok$dqi$1@oravannahka.helsinki.fi...[color=green]
> > Vu Pham <vu@sivell.com> scribbled the following
> > on comp.lang.c:[color=darkred]
> > > I think this problem relates to either c or c++ ( but I am not sure[/color][/color]
> which[color=green][color=darkred]
> > > one ) so I post to both of these news group. I am sorry if I did[/color][/color]
> something[color=green][color=darkred]
> > > wrong here.[/color]
> >[color=darkred]
> > > Is there any difference between these two declarations :[/color]
> >[color=darkred]
> > > 1.
> > > void * functionA( char * p, int s, int * e );[/color]
> >[color=darkred]
> > > and
> > > 2.
> > > typedef void * ( *functionA_t)( char * p, int s, int * e );
> > > functionA_t functionA();[/color]
> >
> > 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.[/color]
>
> 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 ?[/color]

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!


Joona I Palaste
Guest
 
Posts: n/a
#12: Jul 22 '05

re: typedef and declaration of function


Ron Natalie <ron@sensor.com> scribbled the following
on comp.lang.c:[color=blue]
> "Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message news:bu49ok$dqi$1@oravannahka.helsinki.fi...[color=green]
>> 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.[/color][/color]
[color=blue]
> Actually it's a function taking NO arguments (not an unspecified number
> of arguments) in C++. The original poster needs to figure out what language
> he is programming in.[/color]

I said that it's a function taking no arguments in C++, but you snipped
that part away.

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"All that flower power is no match for my glower power!"
- Montgomery Burns
Martin Ambuhl
Guest
 
Posts: n/a
#13: Jul 22 '05

re: typedef and declaration of function


Ron Natalie wrote:
[color=blue]
> "Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message news:bu49ok$dqi$1@oravannahka.helsinki.fi...
>[color=green]
>>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.[/color]
>
>
> Actually it's a function taking NO arguments (not an unspecified number
> of arguments) in C++. The original poster needs to figure out what language
> he is programming in.[/color]

Are you in such a hurry to post that you don't bother reading what you're
responding to? Joona very clearly stated -- in the same post that you
snipped it from to quote the above --[color=blue]
> This smacks of C++, which is also off-topic here. But I can say that
> the same I said above applies for C++, with one difference: replace
> "an unspecified number of arguments" with "no arguments".[/color]
The "here" refers, of course, to comp.lang.c, where Joona read the original
post.



--
Martin Ambuhl
Vu Pham
Guest
 
Posts: n/a
#14: Jul 22 '05

re: typedef and declaration of function



"Vu Pham" <vu@sivell.com> wrote in message
news:bu4ajo$dknat$1@ID-219297.news.uni-berlin.de...[color=blue]
>[/color]
[...]
[color=blue][color=green][color=darkred]
> > > Is there any difference between these two declarations :[/color]
> >[color=darkred]
> > > 1.
> > > void * functionA( char * p, int s, int * e );[/color]
> >[color=darkred]
> > > and
> > > 2.
> > > typedef void * ( *functionA_t)( char * p, int s, int * e );
> > > functionA_t functionA();[/color][/color][/color]

[...][color=blue]
> 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.[/color]


Agrr, I know I need to eat a whole shark to improve my brain.

Looking back to my typedef, I did something basically wrong : there is an
extra * . The typedef should have been

typedef void * ( functionA_t)( char * p, int s, int * e );

and then I can use :
functionA_t functionA; // <---- this will be the declaration for
functionA

For all other variables ( used somewhere else in the app ) that are pointer
to that functions, then I will add the *, like fucntionA_t * aFuncA;

Thanks for all of your advice,

Vu




Vu Pham
Guest
 
Posts: n/a
#15: Jul 22 '05

re: typedef and declaration of function



"Ron Natalie" <ron@sensor.com> wrote in message
news:400565ec$0$71404$9a6e19ea@news.newshosting.co m...[color=blue]
>
> "Vu Pham" <vu@sivell.com> wrote in message[/color]
news:bu49bv$dddep$1@ID-219297.news.uni-berlin.de...[color=blue][color=green]
> >
> > 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();[/color]
>
> Yes, the second defines a function called functionA that returns a pointer
> to function. functionA_t is type pointer to function.
>
> You could
> typedef void (functionA_t) (char*, int, int*);
> functionA_t functionA; // note no parens.
>[/color]

Thanks, Ron. I think you mean

typedef void* (functionA_t) (char*, int, int*);


Vu


Ron Natalie
Guest
 
Posts: n/a
#16: Jul 22 '05

re: typedef and declaration of function



"Vu Pham" <vu@sivell.com> wrote in message news:bu4b4v$do1q2$1@ID-219297.news.uni-berlin.de...
[color=blue]
>
> I use c++ for the implementation file, but that function needs to be
> exported ( from an.so ) and I use extern "C" { } in the declaration file (
> .h ) to prevent the name mangling.[/color]

extern "C" doesn't change the language. Only the linkage. In C++ that's
a function with no arguments.

Nick Hounsome
Guest
 
Posts: n/a
#17: Jul 22 '05

re: typedef and declaration of function



"Vu Pham" <vu@sivell.com> wrote in message
news:bu4dee$duipe$1@ID-219297.news.uni-berlin.de...[color=blue]
>
> "Ron Natalie" <ron@sensor.com> wrote in message
> news:400565ec$0$71404$9a6e19ea@news.newshosting.co m...[color=green]
> >
> > "Vu Pham" <vu@sivell.com> wrote in message[/color]
> news:bu49bv$dddep$1@ID-219297.news.uni-berlin.de...[color=green][color=darkred]
> > >
> > > 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();[/color]
> >
> > Yes, the second defines a function called functionA that returns a[/color][/color]
pointer[color=blue][color=green]
> > to function. functionA_t is type pointer to function.
> >
> > You could
> > typedef void (functionA_t) (char*, int, int*);
> > functionA_t functionA; // note no parens.
> >[/color]
>
> Thanks, Ron. I think you mean
>
> typedef void* (functionA_t) (char*, int, int*);[/color]

Or even just
typedef void* functionA_t(char*,int,int*);

But note that you cannot use the typedef to define the function:

functionA_t f; // OK decl
functionA_t f { } // ill formed defn


Closed Thread