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.

#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 
}
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$

Something like this?

typedef float (*psraBProc)( int x );

float psraBFunc( int x )
{
}

int r;
psraBProc psraB;
float f;

psraB = psraBFunc;
f = psraB( r );

I suggest you take a read through The Function Pointer Tutorials, particularly this one.

I would use some typedefs to help:

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' ); */

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++.

commented: Excellent stuff +36
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.