LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-29-2011, 06:25 AM   #1
LoDam
LQ Newbie
 
Registered: Jan 2011
Location: Fr
Distribution: ArchLinux at the moment
Posts: 25

Rep: Reputation: 4
C++ : function adress/ Variable adress


Hi LQ,

I have a question regarding function adress.
Maybe it is easier to understand my problem with a bit of code:

Code:
#include <iostream>
  2 using namespace std;
  3 void func(void)
  4 {
  5     cout << "Did you call me?" << endl;
  6 return;
  7 }
  8 int main(int argc,char* argv[])
  9 {
 10     void (*f)() = &func;
 11     int i = 0;
 12 
 13     cout
 14         << "without casting :" << endl
 15         << "f: " << f  << " main: " << main << endl
 16         << "with casting :" << endl
 17         << "f:  " << (long)f << " main: " << (long)main
 18         << endl;
 19     cout << "With variables: " << endl << &i << " " << long(&i) << endl;
 20 return 0;
 21 }
and the output I get :

Code:
[Alpha][Tests] ./test_pointfunc
without casting :
f: 1 main: 1
with casting :
f:  4196516 main: 4196550
With variables: 
0x7fff10662df4 140733468519924

Why do I get a simple '1' for the function adress when not casted to couble? (while I get an hex adress for a variable). Also, why is it the same result for all the function (I did it here with the main, but if I had another function, I get the same results).

Is it machine specific, is it that I did something wrong, or is it something I didn't understand?

Cheers,
Loic.
 
Old 03-29-2011, 06:41 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
The function pointer is interpreted as a bool when passed to the ostream object. You can verify this with:
Code:
#include <iostream>

void func(void)
{
}

int main()
{
   void (*f)() = func;
   void (*g)() = NULL;

   std::cout << "f = " << f << std::endl;
   std::cout << "g = " << g << std::endl;
}
Suffice to say, your question has been asked before; read here for the response.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Centos ip adress zerocool22 Linux - Networking 3 04-16-2008 01:40 PM
How to find my ip adress? erpe Debian 4 10-21-2006 05:24 PM
Using 2 computers with 1 Ip adress dasy2k1 Linux - Networking 3 12-01-2005 04:16 PM
View IP-adress pernestor Linux - Networking 2 09-29-2004 09:16 AM
Reporting IP adress eclapton1 Programming 1 06-28-2004 12:34 AM


All times are GMT -5. The time now is 09:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration