what is the target of pointer to function?
when do i use pointer to function?
:?:

Dani AI

Generated

A pointer to function stores the address of a function — its target is the callable code (the function entry point), not any data the function handles and not the function's return value. The pointer type encodes the return type and parameter types; calling through a pointer whose type does not match the actual function is undefined behavior and will often crash or corrupt the stack.

Common uses: callbacks (event handlers, comparators for sort), dispatch tables to replace big switch statements, simple plugin APIs, state machines, and passing behavior into generic routines. Typedefs make declarations easier to read and arrays of function pointers give a compact dispatch mechanism. As asked about the target, that is the function itself; as and pointed out, reference material is useful for edge cases — the short practical guide below shows typical syntax and common pitfalls.

Example (declaration, assignment, call):

int add(int a, int b) { return a + b; }

typedef int (*binop_t)(int,int);

binop_t op = add;      /* assign */
int r = op(2,3);       /* call; same as (*op)(2,3) */

Troubleshooting notes: always declare prototypes so signatures match; on some platforms calling conventions differ (stdcall vs cdecl) and will break calls if mismatched; converting between function pointers and data pointers is non-portable — avoid it unless platform docs explicitly allow it.

Recommended Answers

All 2 Replies

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.