screwed here!!!!!

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

Join Date: Jul 2009
Posts: 17
Reputation: ayan2587 is an unknown quantity at this point 
Solved Threads: 0
ayan2587 ayan2587 is offline Offline
Newbie Poster

screwed here!!!!!

 
0
  #1
23 Days Ago
[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]
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 352
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 53
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
1
  #2
23 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,254
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 155
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso
 
0
  #3
23 Days Ago
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.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*]
      [*solved by : murtan]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 17
Reputation: ayan2587 is an unknown quantity at this point 
Solved Threads: 0
ayan2587 ayan2587 is offline Offline
Newbie Poster
 
0
  #4
23 Days Ago
Originally Posted by firstPerson View Post
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..
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,254
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 155
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso
 
0
  #5
23 Days Ago
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 :

  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.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*]
      [*solved by : murtan]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 17
Reputation: ayan2587 is an unknown quantity at this point 
Solved Threads: 0
ayan2587 ayan2587 is offline Offline
Newbie Poster
 
0
  #6
22 Days Ago
thanks buddy...
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 327
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 59
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz
 
2
  #7
22 Days Ago
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:
  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:
  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:
  1. float doSomething(string str, int index);
we can pass a pointer to it into anotherFunction like this:
  1. anotherFunction(doSomething);

And I think that's about it!
Cheers for now,
Jas.
Last edited by JasonHippy; 22 Days Ago at 1:54 pm.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 17
Reputation: ayan2587 is an unknown quantity at this point 
Solved Threads: 0
ayan2587 ayan2587 is offline Offline
Newbie Poster
 
0
  #8
22 Days Ago
Originally Posted by JasonHippy View Post
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:
  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:
  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:
  1. float doSomething(string str, int index);
we can pass a pointer to it into anotherFunction like this:
  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...
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC