GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing fuckingfunctionpointers.com — How Do I Declare A Function Pointer in C?

How Do I Declare a Function Pointer in C?

As a variable:

returnType (*variableName)(parameterTypes) = function_name;
(example code)

As a static const variable:

static returnType (* const variableName)(parameterTypes) = function_name;
(example code)

As an array:

returnType (*arrayName[])(parameterTypes) = {function_name0, ...};
(example code)

As a parameter to a function:

int my_function(returnType (*parameterName)(parameterTypes));
(example code)

As a return value from a function:

returnType (*my_function(int, ...))(parameterTypes);
(example code)

As a cast (but try not to cast functions):

... (returnType (*)(parameterTypes))my_expression ...
(example code)

As a function pointer typedef:

typedef returnType (*typeName)(parameterTypes);
(example code)

As a function typedef:

typedef returnType typeName(parameterTypes);
(example code)

How Do I Do It in C++? (C++11 and later)

C++ offers several compelling alternatives to C-style function pointers such as templates and std::function with std::bind. If you really want to use function pointers in C++, you can still use the same C-style syntax shown above or the type aliases below.

As a function pointer type alias:

using typeName = returnType (*)(parameterTypes);
(example code)

As a function type alias:

using typeName = returnType (parameterTypes);
(example code)
This site is not intended to be an exhaustive list of all possible uses of function pointers.
If you find yourself needing syntax not listed here, it is likely that a typedef would make your code more readable.

Unable to access this site due to the profanity in the URL? https://goshdarnfunctionpointers.com is a more work-friendly mirror.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.