why the code can compile -- about function pointer

G

George2

Hello everyone,


I am learning code from others, but I do not know why the following
code section can compile?

Why assignment p = new (void (*[3])()) is ok? Are the left side and
right side of assignment having compatible type?

Code:
void (**p)();

int main()
{
	p = new (void (*[3])());
	return 0;
}


thanks in advance,
George
 
R

Rolf Magnus

George2 said:
Hello everyone,


I am learning code from others, but I do not know why the following
code section can compile?

Why assignment p = new (void (*[3])()) is ok? Are the left side and
right side of assignment having compatible type?

Not just compatible. They have the very same type.
Code:
void (**p)();[/QUOTE]

So it's a pointer to pointer to function taking no parameters and returning
void.
[QUOTE]
int main()
{
p = new (void (*[3])());[/QUOTE]

Here you create an array of 3 pointers to functions taking no parameters and
returning void. The operator new returns a pointer to the first element of
that array, which happens to have the same type as p.
[QUOTE]
return 0;
}

A simpler example that is similar in that regard would be one that creates
an array of int instead of an array of pointers to functions:

int *p;

int main()
{
p = new int[3];
return 0;
}
 
B

Bo Persson

George2 said:
Hello everyone,


I am learning code from others, but I do not know why the following
code section can compile?

Why assignment p = new (void (*[3])()) is ok? Are the left side and
right side of assignment having compatible type?

Code:
void (**p)();

int main()
{
p = new (void (*[3])());
return 0;
}

Why bother?

In real code you don't want to do this, ever!


Bo Persson
 

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,183
Messages
2,570,968
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top