473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

[newbie] de-reference function

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
Sep 7 '08 #1
8 1390
Tony Winslow wrote:
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.
Sep 7 '08 #2
red floyd wrote:
Tony Winslow wrote:
>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.
I could be mistaken, but doesn't dereferencing a function pointer yield
the very same function pointer, which explains why the code works?

Here's another example:
void f() {}

int main()
{
void (*ptr)();

ptr = f;
ptr = *f;
ptr = **f;
}
--
Christian Hackl
Sep 7 '08 #3
red floyd wrote:
Tony Winslow wrote:
>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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 7 '08 #4
Christian Hackl wrote:
red floyd wrote:
>Tony Winslow wrote:
>>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.

I could be mistaken, but doesn't dereferencing a function pointer yield
the very same function pointer, which explains why the code works?

Here's another example:
void f() {}

int main()
{
void (*ptr)();

ptr = f;
ptr = *f;
ptr = **f;
}
Actually, it would be funnier if you did

ptr = f;

ptr = *ptr; // dereference and assign again
ptr = *ptr; // and again
ptr = ******************************ptr; // and more...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 7 '08 #5
Victor Bazarov wrote:
red floyd wrote:
>Tony Winslow wrote:
>>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.

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.
Sep 7 '08 #6
On Sep 7, 11:15*pm, Tony Winslow <tonywinslow1...@gmail.comwrote:
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
Sep 8 '08 #7
Victor Bazarov wrote:
red floyd wrote:
>Tony Winslow wrote:
>>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.

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
Sep 8 '08 #8
to***********@yahoo.co.uk wrote:
>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"...
...
Except that if won't work with overloaded functions. Those will have to
remain "unrated"...
Sep 8 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: r_ahimsa_m | last post by:
Hello, I am learning PHP5 in Fedora Linux. I have installed xdebug and Quanta+ editor. Could you tell me please how to start debugging. I can't find description in Internet. THANX /RAM/
16
by: Raxit | last post by:
Hi, i was reading/learning some hello world program in python. I think its very simillar to Java/C++/C#. What's different (except syntax) ? what can i do easily with python which is not easy...
4
by: Johannes Bauer | last post by:
Hello group, I'm currently doing something like this: import time localtime = time.localtime(1234567890) fmttime = "%04d-%02d-%02d %02d:%02d:%02d" % (localtime, localtime, localtime,...
44
by: Andy Dingley | last post by:
On 15 May, 04:55, Prisoner at War <prisoner_at_...@yahoo.comwrote: Or in another universe, where things are understood and site code is stable and reliable, beginners don't even think about...
4
by: r_ahimsa_m | last post by:
Hello, I am learning WWW technologies in Linux. I created index.html file which I can browse with Firefox/Konqueror using URL localhost/~robert/rozgloszenia/index.html. The page looks fine but...
3
by: HansWernerMarschke | last post by:
// This will be a simple example for Matlab programming in C // But I have to first test the C program before I incorporate it into Matlab char *encrypt_string (char *string) { unsigned int i;...
23
by: student.matt | last post by:
ok i am trying to solve this little problem i have my program waits for the 1st system() call to finish before starting the next i will just throw an example of what i mean out in this example...
4
by: ckkwan | last post by:
My VS2008 just arrive. One very dumb question, how to subscribe to event like Page_Init? I can't find it anywhere in the IDE. Ended up have to type in myself. TIA
0
by: Manuel Ebert | last post by:
Hi sharon, the problem is here that a = 12,123 will actually create a tuple with two elements (namely 12 and 123): (12, 123) Converting this to a string yields '(12, 123)', which is not what...
8
by: Dan Christensen | last post by:
Hi, I am an amateur software developer, and would like to know if some freeware I developed a few years ago using VB6 and XP will work under Vista. If so, what must I do to create a Vista...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.