Please tell me ..what do you mean by following line

void(*fnctn)(void(*)(int *,void **),int(*)(void**,int*));

Recommended Answers

All 9 Replies

fnctn is a function pointer, this is given away by the syntax (*fnctn) . It's a type of pointer that points to a function instead of a variable, as in the case of a normal pointer. Here, it says that fnctn points to a function that returns void (since the type before the function pointer is the return-type of the function that it points to) and the function that it points to takes two arguments. I'm not sure about the syntax of the next bit. The arguments of the function pointed to by fnctn might also be function pointers, but I'm not sure of the meaning of (*) , maybe someone else knows more about this?

Anyway, search for "c++ function pointer" on your favourite search engine and I'm sure you'll find what you need eventually. Also, if you find out what the bit that I didn't know is, then post back here with the answer :)

void(*fnctn)(void(*)(int *,void **),int(*)(void**,int*));

Boy, that is pretty messy, always remember, CODE blocks and whitespace are your friends. (Although I don't think any amount of whitespace will help this...)

I think you may be correct, ravenous. I don't mess with them much either, but it sure looks like it's a prototype declaring a pointer to a void function that takes as arguments:

  1. a pointer to a void function requiring 2 arguments
  2. a pointer to an int function requiring 2 arguments

It looks like it's declaring a pointer to a void function that takes as arguments:

  1. a pointer to a void function
  2. a pointer to an int function

Ah yes, I get it now, the (*) bit as an argument is because it's a function prototype (semi-colon at the end of the line), so this is like when you declare a normal function as:

void my_function(int);

you don't need the name of the argument in the prototype, just the type.

please be clear...i m nt getting nything...hw we can have a function inside a function

please be clear...i m nt getting nything...hw we can have a function inside a function

It's not an actual function, it's a pointer to a function. You should read about how machine code actually works in more detail, but basically, when you compile your code into an executable, a function ends up being just another thing that starts at a memory address, somewhere in the computer. Like any other memory address, you can point to it. That's what a function pointer does, it's a pointer that points to the start of the function in the memory, just like int * points to the start of an int .

please be clear...i m nt getting nything...hw we can have a function inside a function

It's not actually a "function inside a function". It's using functions as arguments for another function.

When you do that, the return value from the argument function(s) is used as the value of the argument/parameter for the function being called.

#include <iostream>
using namespace std;

int return2() { return 2; }
int return3() { return 3; }
int returnSum(const int, const int);

int main() {
  const int arg1 = 5, arg2 = 7;
  int constSum = 0, funcSum = 0;

  cout << "Initial value of constSum is: " << constSum << endl;
  cout << "Calling returnSum() with constant arguments arg1 and arg2..." << endl;
  constSum = returnSum(arg1, arg2);
  cout << "The value of constSum has changed to: " << constSum << endl << endl;

  cout << "Initial value of funcSum is: " << funcSum << endl;
  cout << "Calling returnSum() with arguments return2() and return3()..." << endl;
  funcSum = returnSum(return2(), return3());
  cout << "The value of funcSum has changed to: " << funcSum << endl;

  return 0;
}

int returnSum(const int param1, const int param2) {
  return (param1 + param2);
}

void(*fnctn)(void(*)(int *,void **),int(*)(void**,int*)); is equivalent to:

typedef void first_arg_fn_type( int*, void** ) ;

   typedef int second_arg_fn_type( void**, int* ) ;

   typedef void function_type( first_arg_fn_type*, second_arg_fn_type* ) ;

   function_type* fnctn ;

vijayan -since first funtion is a void ...so it will be a raw pointer...so dont we have to convert it to int or something??

void is just the return type of the function; the actual argument is a pointer to such a function.

#include <iostream>
typedef void first_arg_fn_type( int*, void** ) ;
typedef int second_arg_fn_type( void**, int* ) ;
typedef void function_type( first_arg_fn_type*, second_arg_fn_type* ) ;
function_type* fnctn ;

void function_one( int*, void** ) { std::cout << "function_one( int*, void** )\n" ; }
int function_two( void**, int* ) { std::cout << "function_two( void**, int* )\n" ; }

void call_both( first_arg_fn_type* f1, second_arg_fn_type* f2 )
{
    std::cout << "call_both calling f1 => " ;
    f1(0,0) ;
    std::cout << "call_both calling f2 => " ;
    f2(0,0) ;
}


int main( int argc, char** argv )
{
    fnctn = &call_both ;
    first_arg_fn_type* pfn1 = &function_one ;
    second_arg_fn_type* pfn2 = &function_two ;
    fnctn( pfn1, pfn2 ) ;
}
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.