Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typedef and declaration of function

Reply
Thread Tools

typedef and declaration of function

 
 
Vu Pham
Guest
Posts: n/a
 
      01-14-2004
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


 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      01-14-2004
Vu Pham <> 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 () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'I' is the most beautiful word in the world."
- John Nordberg
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      01-14-2004
Joona I Palaste <> scribbled the following
on comp.lang.c:
> Vu Pham <> 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 () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"C++. C++ run. Run, ++, run."
- JIPsoft
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      01-14-2004
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?

 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      01-14-2004
red floyd <> 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 () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Normal is what everyone else is, and you're not."
- Dr. Tolian Soran
 
Reply With Quote
 
Vu Pham
Guest
Posts: n/a
 
      01-14-2004

"Joona I Palaste" <> wrote in message
news:bu49ok$dqi$...
> Vu Pham <> 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


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-14-2004

"Vu Pham" <> wrote in message news:bu49bv$dddep$...
>
> 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.

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-14-2004

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


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-14-2004

"Joona I Palaste" <> wrote in message news:bu49ok$dqi$...
> 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.

 
Reply With Quote
 
Vu Pham
Guest
Posts: n/a
 
      01-14-2004

"Ron Natalie" <> wrote in message
news:400566ab$0$71362$ m...
>
> "Joona I Palaste" <> wrote in message

news:bu49ok$dqi$...
> > 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
Can a static function declaration conflict with a non-static declaration? nospam_timur@tabi.org C Programming 4 12-12-2006 10:26 PM
Variable declaration taken as a function pointer declaration Bolin C++ 4 12-02-2005 05:28 PM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM
typedef and declaration of function Vu Pham C Programming 17 01-15-2004 01:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57