944,181 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 526
  • C++ RSS
Nov 11th, 2009
0

screwed here!!!!!

Expand Post »
[code]
int fun(int(*)());

int main()
{
fun(main);
cout<<"in main"<<endl;
return 0;
}

fun(int (*p)())
{
cout<<"in fun"<<endl;
return 0;
}


the output is....

in fun
in main

how and in which manner is "main" being passed in the fun function argument in main function.
does this way of passing automatically mean that a function pointer that returns an integer is being passed into the fun function argument????

how does this program work..
kindly help..
Thnx
[icode]
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
ayan2587 is offline Offline
60 posts
since Jul 2009
Nov 11th, 2009
1
Re: screwed here!!!!!
the function fun() takes a funtion(whose return type is int and with no params) pointer as its argument.
And u are sending main to that funcion.
Thats it.
Read on pointer to function.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Nov 11th, 2009
0
Re: screwed here!!!!!
How about you warp your code into code tags, correctly.

Then comment on each line of code, so we can see what you are thinking.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008
Nov 11th, 2009
0
Re: screwed here!!!!!
How about you warp your code into code tags, correctly.

Then comment on each line of code, so we can see what you are thinking.

[code]
int fun(int(*)());

int main()
{
fun(main); // the problem is over here...i cant understand how this main is gettin passed to fun??? is it a pointer to main function??
cout<<"in main"<<endl;
return 0;
}

fun(int (*p)())
{
cout<<"in fun"<<endl;
return 0;
}


the output is....

in fun
in main

how and in which manner is "main" being passed in the fun function argument in main function.
does this way of passing automatically mean that a function pointer that returns an integer is being passed into the fun function argument????

how does this program work..
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
ayan2587 is offline Offline
60 posts
since Jul 2009
Nov 11th, 2009
0
Re: screwed here!!!!!
Think of main as a variable. And your function takes a variable a well.
But the variable is not normal, its a function. A function can be passed to another function like a variable. You just need proper prototype.

Compare :

C++ Syntax (Toggle Plain Text)
  1. void Foo(int var); //takes in a int variable
  2. void Foo2( int(*pF)() ); //takes in a int function with no parameters

You can also use typedef to make the function pointer look more
natural.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008
Nov 11th, 2009
0
Re: screwed here!!!!!
thanks buddy...
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
ayan2587 is offline Offline
60 posts
since Jul 2009
Nov 11th, 2009
2
Re: screwed here!!!!!
dkalita has already explained it to you...Try reading his post again!

The function 'fun' returns an int.
But it takes a pointer to a function as a parameter.
The function pointer must point to a function which returns an int and takes no parameters.

main happens to be a function, it returns an int and it takes no parameters, so you can easily pass a pointer to main into the function. Even from within main!

Function pointers do have their uses and I've never seen a function which took a pointer to main as a parameter, but I guess there may be some practical applications, perhaps some recursive algorithms might call for something like this.

I've expanded on the example you posted. This little doozy uses a function pointer to implement recursion:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // function declaration
  6. int myFunction(int (*funcPtr)());
  7.  
  8. // global static int to stop us from recursing too far
  9. static unsigned int count=0;
  10.  
  11. int main()
  12. {
  13. cout << "inside main" << endl;
  14.  
  15. // now we'll call our function passing a pointer to main
  16. myFunction(main);
  17.  
  18. cout << "exiting main" << endl;
  19. return 0;
  20. }
  21.  
  22. // myFunction:
  23. // calls a function pointed to by the input parameter (a function pointer)
  24. // \returns<int>
  25. // \param<funcPtr>takes a pointer to a function which returns an int but takes no parameters
  26. int myFunction(int (*funcPtr)())
  27. {
  28. cout << "inside myFunction!" << endl;
  29.  
  30. // increment the counter
  31. count++;
  32. // if count is less than 3:
  33. if(count<3)
  34. (*funcPtr)(); //call the function being pointed to
  35. //....so main is called again...
  36.  
  37. cout << "exiting myFunction" << endl;
  38. return 0;
  39. }

All I've done there, I've given the function and its parameter slightly more meaningful names, I've also added some extra couts.
You'll also note that the function pointed to by funcPtr is called inside myFunction.
I've also added a static int to stop the program from recursing too far (without the limit in there the program would recurse until it ran out of memory and crashed)

So let's see what's happening when you run the above program, (ignoring the extra couts):
inside 1st instance of main:
call myFunction passing main as a parameter
	inside 1st instance of myFunction:
	count=1
      	function at funcPtr is called.. (i.e. main!)
         	inside 2nd instance of main:
		call myFunction passing main as a parameter
           		inside 2nd instance of myFunction:
             		count=2
             		function at funcPtr is called...
               			inside 3rd instance of main:
                 		call myFunction passing main as a parameter
                   			inside 3d instance of myFunction:
                   			count = 3
                   			count is 3, so function at funcPtr is not called
                   			3rd myFunction returns 0
				3rd main returns 0
			2nd myFunction returns 0
		2nd main returns 0
	1st myFunction returns 0
1st main returns 0

I hope that hasn't confused you too much, I hope you followed that ok.

Take a look at this function definition:
C++ Syntax (Toggle Plain Text)
  1. int anotherFunction(float(*pFunc)(string, int));
This function returns an int and it takes a function pointer as a parameter.
The function pointer in this case takes a pointer to a function which returns a float and takes a string and an int as parameters.

So anotherFunction can be passed a pointer to any function which returns float and takes a string and an int as parameters.

so if we had a function defined like so:
C++ Syntax (Toggle Plain Text)
  1. float doSomething(string str, int index);
we can pass a pointer to it into anotherFunction like this:
C++ Syntax (Toggle Plain Text)
  1. anotherFunction(doSomething);

And I think that's about it!
Cheers for now,
Jas.
Last edited by JasonHippy; Nov 11th, 2009 at 1:54 pm.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Nov 11th, 2009
0
Re: screwed here!!!!!
Click to Expand / Collapse  Quote originally posted by JasonHippy ...
dkalita has already explained it to you...Try reading his post again!

The function 'fun' returns an int.
But it takes a pointer to a function as a parameter.
The function pointer must point to a function which returns an int and takes no parameters.

main happens to be a function, it returns an int and it takes no parameters, so you can easily pass a pointer to main into the function. Even from within main!

Function pointers do have their uses and I've never seen a function which took a pointer to main as a parameter, but I guess there may be some practical applications, perhaps some recursive algorithms might call for something like this.

I've expanded on the example you posted. This little doozy uses a function pointer to implement recursion:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // function declaration
  6. int myFunction(int (*funcPtr)());
  7.  
  8. // global static int to stop us from recursing too far
  9. static unsigned int count=0;
  10.  
  11. int main()
  12. {
  13. cout << "inside main" << endl;
  14.  
  15. // now we'll call our function passing a pointer to main
  16. myFunction(main);
  17.  
  18. cout << "exiting main" << endl;
  19. return 0;
  20. }
  21.  
  22. // myFunction:
  23. // calls a function pointed to by the input parameter (a function pointer)
  24. // \returns<int>
  25. // \param<funcPtr>takes a pointer to a function which returns an int but takes no parameters
  26. int myFunction(int (*funcPtr)())
  27. {
  28. cout << "inside myFunction!" << endl;
  29.  
  30. // increment the counter
  31. count++;
  32. // if count is less than 3:
  33. if(count<3)
  34. (*funcPtr)(); //call the function being pointed to
  35. //....so main is called again...
  36.  
  37. cout << "exiting myFunction" << endl;
  38. return 0;
  39. }

All I've done there, I've given the function and its parameter slightly more meaningful names, I've also added some extra couts.
You'll also note that the function pointed to by funcPtr is called inside myFunction.
I've also added a static int to stop the program from recursing too far (without the limit in there the program would recurse until it ran out of memory and crashed)

So let's see what's happening when you run the above program, (ignoring the extra couts):
inside 1st instance of main:
call myFunction passing main as a parameter
	inside 1st instance of myFunction:
	count=1
      	function at funcPtr is called.. (i.e. main!)
         	inside 2nd instance of main:
		call myFunction passing main as a parameter
           		inside 2nd instance of myFunction:
             		count=2
             		function at funcPtr is called...
               			inside 3rd instance of main:
                 		call myFunction passing main as a parameter
                   			inside 3d instance of myFunction:
                   			count = 3
                   			count is 3, so function at funcPtr is not called
                   			3rd myFunction returns 0
				3rd main returns 0
			2nd myFunction returns 0
		2nd main returns 0
	1st myFunction returns 0
1st main returns 0

I hope that hasn't confused you too much, I hope you followed that ok.

Take a look at this function definition:
C++ Syntax (Toggle Plain Text)
  1. int anotherFunction(float(*pFunc)(string, int));
This function returns an int and it takes a function pointer as a parameter.
The function pointer in this case takes a pointer to a function which returns a float and takes a string and an int as parameters.

So anotherFunction can be passed a pointer to any function which returns float and takes a string and an int as parameters.

so if we had a function defined like so:
C++ Syntax (Toggle Plain Text)
  1. float doSomething(string str, int index);
we can pass a pointer to it into anotherFunction like this:
C++ Syntax (Toggle Plain Text)
  1. anotherFunction(doSomething);

And I think that's about it!
Cheers for now,
Jas.

Thanx a lot buddy...
i really appreciate ur going to such lengths to explain the concept...
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
ayan2587 is offline Offline
60 posts
since Jul 2009

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: How do I print in terminal?
Next Thread in C++ Forum Timeline: very basic c++ question on vectors





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


Follow us on Twitter


© 2011 DaniWeb® LLC