| | |
Pointer to function returning a pointer to function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 66
Reputation:
Solved Threads: 0
Question: Declare a pointer to a function taking an int argument and returning a pointer to a function that takes a char argument and returns a float.
I did code as follows. But it is giving error. Please correct this.
I did code as follows. But it is giving error. Please correct this.
C Syntax (Toggle Plain Text)
#include <iostream> using namespace std; float func(int x)(char c) { cout << "func() called..." << endl; } int main() { int x = 2; char c; float (*(*fp)(int))(char); // Define a function pointer (*fp) = func; // Initialize it (*(*fp))(x)(c); // Dereferencing calls the function }
C++ Syntax (Toggle Plain Text)
ambarish@ubuntu:~/exercise$ g++ ex33.cpp ex33.cpp:4: error: ‘func’ declared as function returning a function ex33.cpp: In function ‘int main()’: ex33.cpp:13: error: assignment of read-only location ‘* fp’ ex33.cpp:13: error: cannot convert ‘int ()(int)’ to ‘float (* ()(int))(char)’ in assignment ambarish@ubuntu:~/exercise$
Ambarish
Something like this?
C Syntax (Toggle Plain Text)
typedef float (*psraBProc)( int x ); float psraBFunc( int x ) { } int r; psraBProc psraB; float f; psraB = psraBFunc; f = psraB( r );
Last edited by wildgoose; Aug 10th, 2009 at 12:56 am.
I suggest you take a read through The Function Pointer Tutorials, particularly this one.
I would use some typedefs to help:
BTW, you really are doing something weird. What goal can you possibly be trying to accomplish? (It appears to me that there is a design flaw in your program.) Perhaps we can suggest something better (and easier).
Also, this is the C forum. You are using C++.
I would use some typedefs to help:
C Syntax (Toggle Plain Text)
typedef float (char2float_t)( char ); typedef char2float_t* int2char2float_t( int ); ... char2float_t* wonky_function_1( int n ) { ... } ... int2char2float_t* fp1 = &wonky_function_1; char2float_t* fp2 = fp1( 42 ); /* or: fp2 = (*fp1)( 42 ); */ float value = fp2( 'A' ); /* likewise: value = (*fp2)( 'A' ); */
Also, this is the C forum. You are using C++.
![]() |
Similar Threads
- *Pointer program problem. (C++)
- How to access a Public Pointer value of a class directly? (C++)
- pointer to member? (C++)
- pointer to some code (C++)
- pointer (C)
- Assign content to a function pointer (C)
- Change Out Your Pointer Scheme (Windows tips 'n' tweaks)
Other Threads in the C++ Forum
- Previous Thread: Help with a counting program
- Next Thread: Bank program help!
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






