473,383 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

typedef and declaration of function

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
16 8759
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
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
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
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

"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

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

"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

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

"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

"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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Vu Pham | last post by:
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...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
5
by: Cancerbero | last post by:
Hi (first, excuse me for my bad english) As I know, the semantics for typedef is: typedef A B; I think this makes B a synonym of A, where A is an existing data type. Is that right? Based...
2
by: Mehta Shailendrakumar | last post by:
Hi all, Have alook at two different declarations below: typedef unsigned char uc; typedef void (*fptr) (void); The first declaration has a left part which is a C keyword: "unsigned char"...
4
by: Alex Vinokur | last post by:
How does typedef work for foo2_t? What can one do with foo2_t? ------ bar.c ------ void foo() {} typedef void (*foo1_t)(); typedef void foo2_t(); /* How does typedef work here? */
14
by: yang__lee | last post by:
Hi, You all know typedef typedef struct g { int a; int b; } google;
3
by: robin liu | last post by:
What's the difference between these two declarations ? 1) typedef void (*pf)(void); 2) typedef void f(void); the first declaration is define a function pointer, what is the second ? define a...
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
4
by: subramanian100in | last post by:
Consider the following: Class Test { public: Test(Test_int arg) : val(arg) { } typedef int Test_int; private:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.