void func(char*,int&);//A function returning no value and taking a char and reference to int as arguments.

typedef void (*PTR)(char*,int&);//A pointer to function returning no value and taking a char and reference to int as arguments.

void Func2(PTR);//A Function taking such a pointer as argument.

PTR Func3();//A Function returning such a pointer.

//Write Definitions of Function taking such a pointer as an Argument and //returning the function.

PTR FUNC4(PTR val)
{
	return val;
}

Apart from that here is another code

typedef int (&rifii) (int,int);

I assume that it is a reference to a function returning int and taking 2 ints as arguments.

I am unable to understand How and where will such a pointer be useful?

Recommended Answers

All 6 Replies

void func(char*,int&);//A function returning no value and taking a char and reference to int as arguments.

If you want a function that takes a char as an argument, you should declare it as void func(char, int&);

If you want a function that takes a char as an argument, you should declare it as void func(char, int&);

Sorry I dint write it correctly over there. it is a char* as argument.

I would like to know about the second doubt also .

>I am unable to understand How and where will such a pointer be useful?
I assume you mean a pointer to a function in general. One example is in the C standard library: qsort. Without using a pointer to a function as a callback, there wouldn't be a good way to make the function generic.

Well I mean to say Where could a reference to a function be useful? I am unable to understand its uses Especially when declared as a TYPEDEF

>Well I mean to say Where could a reference to a function be useful?
Pretty much the same places a pointer to a function is useful. Barring extra restrictions on a reference type (same as references to objects), the two are pretty much identical.

>Especially when declared as a TYPEDEF
The typedef is there for your convenience. Instead of having to work out a complex declaration, you can use typedefs to make the syntax simpler without changing the semantics.

I am unable to understand How and where will such a pointer be useful?

Consider the signal function as an example.

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.