typedef and declaration of function

V

Vu Pham

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
 
J

Joona I Palaste

Vu Pham <[email protected]> scribbled the following
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.
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.

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.
Each of these declaration is embraced by extern "C" { }. The implenetation
file is a .cpp compiled with g++.

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".
 
R

red floyd

Vu said:
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

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?
 
J

Joona I Palaste

red floyd <[email protected]> scribbled the following
It's the same as the difference between:
extern int a;
and
int *a;

No, it isn't.
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*

Note the () after the symbol functionA. This makes functionA
*A FUNCTION* returning the pointer to a function you defined above.
Does that make some sense?

Pretty much... =)
 
V

Vu Pham

Joona I Palaste said:
Vu Pham <[email protected]> scribbled the following





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 ?

Thanks,

Vu
 
R

Ron Natalie

Vu Pham said:
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, 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.
 
R

Ron Natalie

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

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

Ron Natalie

Joona I Palaste said:
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.

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.
 
V

Vu Pham

Ron Natalie said:
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.

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
 
X

xarax

Vu Pham said:
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!
 
J

Joona I Palaste

Ron Natalie <[email protected]> scribbled the following
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.

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

Martin Ambuhl

Ron said:
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.

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 --
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".
The "here" refers, of course, to comp.lang.c, where Joona read the original
post.
 
V

Vu Pham

Vu Pham said:
[...]
[...]
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.


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
 
V

Vu Pham

Ron Natalie said:
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.

Thanks, Ron. I think you mean

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


Vu
 
R

Ron Natalie

Vu Pham said:
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.

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

Nick Hounsome

Vu Pham said:
Thanks, Ron. I think you mean

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

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
 
D

Daniel Haude

On Wed, 14 Jan 2004 14:58:37 -0600,
in Msg. said:
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 ?

Perhaps with a #define:

#define FUNC_ARGS char * p, int s, int * e

typedef void * ( functionA_t)( FUNC_ARGS );
void *functionA(FUNC_ARGS);

--Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top