Pointers to function question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Pointers to function question

 
0
  #1
May 22nd, 2007
Why both of the next examples are the same?

Ex1 : int*(*foo)() = Function;
Ex2: int*(*foo)() = &Function;


Notice: In the second example i'm using & operator.

That means that c++ takes function name as an address of it and this () dereferencing it just like in arrays?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 36
Reputation: azimuth0 is an unknown quantity at this point 
Solved Threads: 5
azimuth0 azimuth0 is offline Offline
Light Poster

Re: Pointers to function question

 
0
  #2
May 22nd, 2007
Yes, that's precisely correct.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,582
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Pointers to function question

 
0
  #3
May 22nd, 2007
Yes, the use of & is optional in this case. Similarly, when calling the function *foo() and foo() will also be the same.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Pointers to function question

 
0
  #4
May 23rd, 2007
Originally Posted by laconstantine View Post
...c++ takes function name as an address of it...
c++ takes the name of the function (as an expression) to be either a reference to the function or a pointer to the function. eg.
  1. #include <iostream>
  2.  
  3. void foo( void(*pfn)() ) { std::cout << "pointer\n" ; }
  4. void foo( void(&rfn)() ) { std::cout << "reference\n" ; }
  5. void bar() { std::cout << "bar\n" ; }
  6.  
  7. int main()
  8. {
  9. bar() ; // ok
  10. (*bar)() ; // ok , bar == &bar, so (*bar) == bar
  11. (**bar)() ; // also ok
  12. (***bar)() ; // also ok
  13.  
  14. foo(bar) ; // error - ambiguous is bar a reference or a pointer?
  15. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: Pointers to function question

 
0
  #5
May 23rd, 2007
Why this is legal?

(****bar)() ;

and here is not?
int x =2;
int *ptr = &x;
cout << (***ptr);

???
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Pointers to function question

 
2
  #6
May 23rd, 2007
You're getting confused between two very distinct topics in C/C++ - Pointers, and function pointers, which are both a rather different kettle of fish.

The reason that function pointers can be handled in such a way as shown in your original post is because of the nature of functions themselves.
in C/C++, you can really only do two things with a function - you may call it, or you may take its address.

Contrast this with a variable, you may also take its address, but you may not call it. In addition to this, unlike functions, you can assign to it, and you can obtain the data it stores.

Since functions and variables are so different, it follows that a pointer-to-function behaves differently to a pointer-to-variable.


Just to put the above into context, when you use the name (identifier) of a function, without the trailing parentheses, the compiler already knows that the function cannot have a value stored, therefore the address of the function is returned instead - the & (address-of) operator is unnecessary.

When you use the name/identifier of a variable, the compiler assumes you wish to obtain its stored value. Hence, if you wish to obtain the address of a variable you must explicitly use the '&' (address-of) operator.


The example you posted using the * (dereference) operator is not legal for the same reasons listed above. this is - a function has no stored data value, so explicitly dereferencing a pointer-to-function does nothing.
On the other hand A variable does store a value, so explicitly de-referencing a pointer-to-variable does have an effect (the effect is to return the variable itself). Ordinary non-pointer variables may not be de-referenced (This would be a nonsensical operation)
Last edited by Bench; May 23rd, 2007 at 9:07 am.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Pointers to function question

 
1
  #7
May 23rd, 2007
Originally Posted by laconstantine View Post
Why this is legal?
(****bar)() ;
  1. void bar() { /* ... */ }
  2.  
  3. int main()
  4. {
  5. void (*pfn)() = bar ; // ok, bar is treated as &bar
  6. void (&rfn)() = *pfn ; // ok, *pfn is a reference to the function
  7. pfn = rfn ; // ok, equivalent to pfn = &rfn ;
  8. pfn = *bar ; // ok, equivalent to pfn = &*&bar ;
  9. pfn = **bar ; // ok, equivalent to pfn = &*&*&bar ;
  10. pfn = ***bar ; // ok, equivalent to pfn = &*&*&*&bar ;
  11. (*pfn)() ; // ok, call bar
  12. (****bar)() ; // ok, equivalent to (*pfn)() ;
  13. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: Pointers to function question

 
0
  #8
May 24th, 2007
So its
(***FuncName)()

Its legal because functions have no values so the dereferencing asterik has no effect on the function name?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Pointers to function question

 
0
  #9
May 24th, 2007
the dereference operator (*) on a pointer gives a reference to what is pointed to. the address of operator on a reference gives a pointer to what is being referred to. since a reference to a function can be implicitly treated as a pointer (an implicit address of is assumed),
*f == *(&f) == f

it really has got nothing to do with values or data. a void* points to some data (we do not know what it's type is), but cannot be dereferenced. an int* points to an int, so dereferencing it gives us a reference to an int (int&).
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Pointers to function question

 
0
  #10
May 24th, 2007
Originally Posted by laconstantine View Post
So its
(***FuncName)()

Its legal because functions have no values so the dereferencing asterik has no effect on the function name?
Pretty much, yes.


although it would be noteworthy to say that code like this doesn't pass QA when it comes to readability.
  1. void foo()
  2. {
  3. }
  4.  
  5. int main()
  6. {
  7. (****foo)();
  8. }
Make sure that when you use the dereference operator, that it actually enhances the readability/meaning of the code, and doesn't detract from it.
Last edited by Bench; May 24th, 2007 at 7:18 am.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC