typedef doubt

N

NG

Hi all,
can anybody explain me the following code snippet.

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

VP vp;

thanks in advance
NG
 
O

osmium

NG said:
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*.
 
J

JKop

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
 
A

Ali Cehreli

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
 
M

Mike Wahler

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

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
 
R

Richard Herring

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

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

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
474,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top