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

typedef doubt

P: n/a
NG
Hi all,
can anybody explain me the following code snippet.

typedef void * (*VP)(char*);

VP vp;

thanks in advance
NG
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
NG writes:
can anybody explain me the following code snippet.

typedef void * (*VP)(char*);

VP vp;


It says that VP can be used as a type identifier for a function that
receives a char * as a parameter and returns a void*.
Jul 22 '05 #2

P: n/a
NG posted:
Hi all,
can anybody explain me the following code snippet.

typedef void * (*VP)(char*);

VP vp;

thanks in advance
NG


VP is a new type. It is a pointer to a function. The
function is like so:

void* FunctionName(char*);

Say for instance you have an actual function:

void* MyFunction(char*)
{
return reinterpret_cast<void*>(52);
}

int main()
{
VP blah = MyFunction;

blah("j");
}
blah is now a pointer to a function.
-JKop
Jul 22 '05 #3

P: n/a
On Thu, 05 Aug 2004 10:51:13 -0700, NG wrote:
can anybody explain me the following code snippet.

typedef void * (*VP)(char*);

VP vp;


There is a tool under Linux (most unixes?) called cdecl, which is used
to explain or declare complex C and C++ types. I don't use it though :)

Using that tool with your declaration outputs

$ cdecl -+ explain 'void * (*VP)(char*);'

declare VP as pointer to function (pointer to char) returning
pointer to void

Having a typedef in your declaration changes the meaning of VP from
pointer to function to the alias of such a type.

We can use cdecl for the declaration of complex types. Using an example
from its man page:

$ cdecl -+ declare 'foo as pointer to member of class X function
(arg1, arg2) returning pointer to class Y'

class Y *(X::*foo)(arg1, arg2);

(Of course the 'class' keyword at the beginning is redundant in C++.)

Ali
Jul 22 '05 #4

P: n/a

"NG" <go******@gmail.com> wrote in message
news:64**************************@posting.google.c om...
Hi all,
can anybody explain me the following code snippet.

typedef void * (*VP)(char*);
Creates a name (an 'alias'), 'VP', for type
"pointer to function taking one type 'char*' argument
and returning type 'void*'".

Look up 'typedef' and 'function pointer' or 'pointer to function'
in a C++ text.
VP vp;


Creates an object of the above type (if this declaration
appears at file scope, also initializes it with a value of
NULL; if at block scope, the object is not given a meaningful
value).

-Mike
Jul 22 '05 #5

P: n/a
In message <5L******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
NG posted:
Hi all,
can anybody explain me the following code snippet.

typedef void * (*VP)(char*);

VP vp;

thanks in advance
NG


VP is a new type.


No. Despite its name, "typedef" does not define new types. VP is a new
name for an existing type (which, as it happens, didn't previously have
a name, unless you count "pointer to function taking pointer to char and
returning pointer to void").

--
Richard Herring
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.