By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,205 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,205 IT Pros & Developers. It's quick & easy.

typedef and declaration of function

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
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.
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".

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'I' is the most beautiful word in the world."
- John Nordberg
Nov 14 '05 #2

P: n/a
Joona I Palaste <pa*****@cc.helsinki.fi> scribbled the following
on comp.lang.c:
Vu Pham <vu@sivell.com> scribbled the following
on comp.lang.c:
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.
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.


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"C++. C++ run. Run, ++, run."
- JIPsoft
Nov 14 '05 #3

P: n/a
Vu Pham wrote:
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?

Nov 14 '05 #4

P: n/a
red floyd <no*****@here.dude> scribbled the following
on comp.lang.c:
Vu Pham wrote:
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++.
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... =)

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Normal is what everyone else is, and you're not."
- Dr. Tolian Soran
Nov 14 '05 #5

P: n/a

"Vu Pham" <vu@sivell.com> wrote in message news:bu************@ID-219297.news.uni-berlin.de...

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.

Nov 14 '05 #6

P: n/a

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bu**********@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 ?

Thanks,

Vu
Nov 14 '05 #7

P: n/a
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.
Nov 14 '05 #8

P: n/a

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message news:bu**********@oravannahka.helsinki.fi...
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.

Nov 14 '05 #9

P: n/a

"Ron Natalie" <ro*@sensor.com> wrote in message
news:40***********************@news.newshosting.co m...

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message news:bu**********@oravannahka.helsinki.fi...
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.


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
Nov 14 '05 #10

P: n/a
"Vu Pham" <vu@sivell.com> wrote in message
news:bu************@ID-219297.news.uni-berlin.de...

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bu**********@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!
Nov 14 '05 #11

P: n/a
Ron Natalie <ro*@sensor.com> scribbled the following
on comp.lang.c:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message news:bu**********@oravannahka.helsinki.fi...
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.


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"All that flower power is no match for my glower power!"
- Montgomery Burns
Nov 14 '05 #12

P: n/a
Ron Natalie wrote:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message news:bu**********@oravannahka.helsinki.fi...
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.


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.

--
Martin Ambuhl
Nov 14 '05 #13

P: n/a

"Vu Pham" <vu@sivell.com> wrote in message
news:bu************@ID-219297.news.uni-berlin.de...
[...]
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();


[...] 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


Nov 14 '05 #14

P: n/a

"Ron Natalie" <ro*@sensor.com> wrote in message
news:40***********************@news.newshosting.co m...

"Vu Pham" <vu@sivell.com> wrote in message

news:bu************@ID-219297.news.uni-berlin.de...

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.


Thanks, Ron. I think you mean

typedef void* (functionA_t) (char*, int, int*);
Vu
Nov 14 '05 #15

P: n/a

"Vu Pham" <vu@sivell.com> wrote in message news:bu************@ID-219297.news.uni-berlin.de...

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.

Nov 14 '05 #16

P: n/a

"Vu Pham" <vu@sivell.com> wrote in message
news:bu************@ID-219297.news.uni-berlin.de...

"Ron Natalie" <ro*@sensor.com> wrote in message
news:40***********************@news.newshosting.co m...

"Vu Pham" <vu@sivell.com> wrote in message

news:bu************@ID-219297.news.uni-berlin.de...

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.


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
Nov 14 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.