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$

Dani AI

Generated

Quick summary: the compiler errors happen because C/C++ do not allow “functions that return functions” — they only allow functions that return pointers to functions (or function objects). Use an alias (or typedef) to make the types readable, assign to the pointer variable itself (not *fp), and prefer std::function or a small functor if you can.

Example (modern C++ for clarity — use typedef if you need pre-C++11):

#include <iostream>

using CharToFloat = float (*)(char);           // pointer: char -> float
using IntToCharToFloat = CharToFloat (*)(int); // pointer: int -> (pointer: char->float)

float charImpl(char c) { return static_cast<float>(c) + 0.5f; }

CharToFloat chooser(int n) {
    // could pick different functions based on n; here always return charImpl
    return &charImpl;
}

int main() {
    IntToCharToFloat fp = &chooser;    // assign the function pointer directly
    CharToFloat p = fp(42);            // call the int->(char->float) function
    if (p) std::cout << p('A') << '\n';
}

Troubleshooting notes tied to the original thread: ’s definition attempted to declare a function returning a function (illegal), which produces “declared as function returning a function.” The “assignment of read-only location ‘fp’” came from writing `(fp) = func;— you should assign the pointer variable (fp = func;orfp = &func;`), not its (non‑assignable) dereference.

Design note: as suggested, typedefs/aliases greatly improve readability. Also consider returning std::function<float(char)> or a small callable object instead of raw function pointers; those options are safer and far easier to maintain than nested pointer syntax.

Recommended Answers

All 3 Replies

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.