[newbie] de-reference function

T

Tony Winslow

Hi,

I'm wondering why the following code works just fine:

#include <stdio.h>

int main()
{
(******printf)("Hello!\n");

return 0;
}

best regards,
Tony Winslow
 
R

red floyd

Tony said:
Hi,

I'm wondering why the following code works just fine:

#include <stdio.h>

int main()
{
(******printf)("Hello!\n");

return 0;
}

I'm not sure. Gcc took it with -ansi -Wall -pedantic
Comeau took it in strict mode.

That looks really wierd to me.
 
R

red floyd

Victor said:
What's so weird? Since the * associate from right to left, you get

*(*(*(*(*(*(printf))))))

The 'printf' expression yields a pointer to function, right? Dereference
it, and you will get... a an lvalue of the function type, right? So,
the first expression

*(printf)

Gives you an lvalue of function type int(char const*,...). There is no
doubt about that, is there? Now, the compiler sees the next dereference
operator. What will it try to do? The dereference operator needs a
pointer as its argument, right? Now, can an lvalue of function type be
converted to some pointer?

According to 4.3, if an lvalue of function type T can be converted to an
rvalue of type pointer-to-T, the result is a pointer to function. So,
the expression

(*printf)

is converted to a pointer to function. The next dereference yields
another lvalue, and so on.

I figured it was something like that, but it's still pretty weird looking.
 
T

tony_in_da_uk

Hi,

I'm wondering why the following code works just fine:

#include <stdio.h>

int main()
{
        (******printf)("Hello!\n");

        return 0;

}

best regards,
Tony Winslow

That's so cool... I'm going to use it to rate how important my
function calls are now... 5 stars is really mega-important... 1 is
kind-of "who cares"...

Cheers,

Tony
 
T

Tony Winslow

Victor said:
What's so weird? Since the * associate from right to left, you get

*(*(*(*(*(*(printf))))))

The 'printf' expression yields a pointer to function, right? Dereference
it, and you will get... a an lvalue of the function type, right? So,
the first expression

*(printf)

Gives you an lvalue of function type int(char const*,...). There is no
doubt about that, is there? Now, the compiler sees the next dereference
operator. What will it try to do? The dereference operator needs a
pointer as its argument, right? Now, can an lvalue of function type be
converted to some pointer?

According to 4.3, if an lvalue of function type T can be converted to an
rvalue of type pointer-to-T, the result is a pointer to function. So,
the expression

(*printf)

is converted to a pointer to function. The next dereference yields
another lvalue, and so on.

V

Wow, I understand it now.
Thanks for your analysis!

Best regards,
Tony Winslow
 
A

Andrey Tarasevich

That's so cool... I'm going to use it to rate how important my
function calls are now... 5 stars is really mega-important... 1 is
kind-of "who cares"...
...

Except that if won't work with overloaded functions. Those will have to
remain "unrated"...
 

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
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top