943,977 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2248
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 22nd, 2007
0

Pointers to function question

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 22nd, 2007
0

Re: Pointers to function question

Yes, that's precisely correct.
Reputation Points: 11
Solved Threads: 5
Light Poster
azimuth0 is offline Offline
36 posts
since May 2006
May 22nd, 2007
0

Re: Pointers to function question

Yes, the use of & is optional in this case. Similarly, when calling the function *foo() and foo() will also be the same.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
May 23rd, 2007
0

Re: Pointers to function question

...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.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
May 23rd, 2007
0

Re: Pointers to function question

Why this is legal?

(****bar)() ;

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

???
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 23rd, 2007
2

Re: Pointers to function question

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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
May 23rd, 2007
1

Re: Pointers to function question

Why this is legal?
(****bar)() ;
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
May 24th, 2007
0

Re: Pointers to function question

So its
(***FuncName)()

Its legal because functions have no values so the dereferencing asterik has no effect on the function name?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 24th, 2007
0

Re: Pointers to function question

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&).
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
May 24th, 2007
0

Re: Pointers to function question

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.
CPP Syntax (Toggle Plain Text)
  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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ A little homework help, Thanks very much for the last assistance
Next Thread in C++ Forum Timeline: C++ homework help on files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC