| | |
screwed here!!!!!
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 17
Reputation:
Solved Threads: 0
[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]
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]
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.
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?
•
•
Join Date: Jul 2009
Posts: 17
Reputation:
Solved Threads: 0
0
#4 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.
[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..
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 :
You can also use typedef to make the function pointer look more
natural.
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)
void Foo(int var); //takes in a int variable 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?
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:
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):
I hope that hasn't confused you too much, I hope you followed that ok.
Take a look at this function definition:
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:
we can pass a pointer to it into anotherFunction like this:
And I think that's about it!
Cheers for now,
Jas.
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)
#include <iostream> using namespace std; // function declaration int myFunction(int (*funcPtr)()); // global static int to stop us from recursing too far static unsigned int count=0; int main() { cout << "inside main" << endl; // now we'll call our function passing a pointer to main myFunction(main); cout << "exiting main" << endl; return 0; } // myFunction: // calls a function pointed to by the input parameter (a function pointer) // \returns<int> // \param<funcPtr>takes a pointer to a function which returns an int but takes no parameters int myFunction(int (*funcPtr)()) { cout << "inside myFunction!" << endl; // increment the counter count++; // if count is less than 3: if(count<3) (*funcPtr)(); //call the function being pointed to //....so main is called again... cout << "exiting myFunction" << endl; return 0; }
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)
int anotherFunction(float(*pFunc)(string, int));
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)
float doSomething(string str, int index);
C++ Syntax (Toggle Plain Text)
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!
Those who understand binary .....
And those who don't!
•
•
Join Date: Jul 2009
Posts: 17
Reputation:
Solved Threads: 0
0
#8 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:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; // function declaration int myFunction(int (*funcPtr)()); // global static int to stop us from recursing too far static unsigned int count=0; int main() { cout << "inside main" << endl; // now we'll call our function passing a pointer to main myFunction(main); cout << "exiting main" << endl; return 0; } // myFunction: // calls a function pointed to by the input parameter (a function pointer) // \returns<int> // \param<funcPtr>takes a pointer to a function which returns an int but takes no parameters int myFunction(int (*funcPtr)()) { cout << "inside myFunction!" << endl; // increment the counter count++; // if count is less than 3: if(count<3) (*funcPtr)(); //call the function being pointed to //....so main is called again... cout << "exiting myFunction" << endl; return 0; }
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:
This function returns an int and it takes a function pointer as a parameter.C++ Syntax (Toggle Plain Text)
int anotherFunction(float(*pFunc)(string, int));
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:
we can pass a pointer to it into anotherFunction like this:C++ Syntax (Toggle Plain Text)
float doSomething(string str, int index);
C++ Syntax (Toggle Plain Text)
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...
![]() |
Similar Threads
- European PS3 users screwed (Upcoming News Stories)
- my system is screwed up! (Windows NT / 2000 / XP)
- Internet Explore is all screwed up! (Web Browsers)
- evidence elinator screwed up my comp...help (Windows NT / 2000 / XP)
- I screwed up? Help. (Storage)
- My moniter is severly screwed up! (Windows NT / 2000 / XP)
- IE wont load because registry got screwed up. (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: How do I print in terminal?
- Next Thread: very basic c++ question on vectors
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






