> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C Function Pointer

A function pointer is a variable that holds the memory address of executable code rather than a data value. In C, functions reside in the text (or code) segment of memory, and a function pointer stores the base address of a specific function's machine instructions, enabling indirect invocation.

## Syntax and Declaration

The declaration of a function pointer must strictly define the signature of the function it points to, including the return type and the parameter types.

```c theme={"dark"}
return_type (*pointer_name)(parameter_type1, parameter_type2, ...);
```

The parentheses surrounding `*pointer_name` are mandatory due to operator precedence. Without them, the compiler interprets the statement as a function declaration returning a standard data pointer.

## Assignment and Invocation

When assigning a function to a function pointer, the function's identifier automatically decays into a pointer to its first instruction, analogous to array decay. The address-of operator (`&`) is optional but often used to explicitly denote intent.

Similarly, when invoking the function via the pointer, explicit dereferencing (`*`) is optional.

```c theme={"dark"}
int add(int a, int b) {
    return a + b;
}

int main() {
    // 1. Declaration
    int (*func_ptr)(int, int);

    // 2. Assignment
    func_ptr = &add;  // Explicit address-of
    func_ptr = add;   // Implicit decay (functionally identical)

    // 3. Invocation
    int res1 = (*func_ptr)(5, 3); // Explicit dereference
    int res2 = func_ptr(5, 3);    // Implicit dereference (functionally identical)

    return 0;
}
```

## Type Aliasing (`typedef`)

Function pointer syntax becomes notoriously difficult to parse when nested, such as when a function returns a function pointer or accepts one as an argument. The `typedef` keyword is used to create a type alias for the function signature, abstracting the pointer syntax.

```c theme={"dark"}
// Defines 'math_op' as a type representing a pointer to a function 
// taking two ints and returning an int.
typedef int (*math_op)(int, int);

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

int main() {
    // Declaration using the typedef
    math_op ptr = add;
    int result = ptr(10, 20);
    
    return 0;
}
```

## Arrays of Function Pointers

Multiple function pointers sharing an identical signature can be stored contiguously in an array. The array subscript operator is evaluated before the function call operator.

```c theme={"dark"}
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int divide(int a, int b) { return a / b; }

int main() {
    // Declaration and initialization of an array of 4 function pointers
    int (*op_array[4])(int, int) = {add, subtract, multiply, divide};

    // Invoking the function at index 2 (multiply)
    int result = op_array[2](10, 5); 
    
    return 0;
}
```

## Memory and Type Safety Constraints

* **Strict Typing:** A function pointer must strictly match the return type and parameter types of the target function. Invoking a function through an incompatible pointer corrupts the call stack and results in undefined behavior.
* **Data Pointer Incompatibility:** The ISO C standard dictates that function pointers and data pointers (such as `void *`) are not guaranteed to be interchangeable or of the same size. Casting between a function pointer and a data pointer is undefined behavior in strict C, though certain POSIX extensions (like `dlsym`) mandate this conversion.
* **Lvalue Constraints:** A dereferenced function pointer yields a function designator, which is not a modifiable lvalue. Attempting to assign a value to a dereferenced function pointer (e.g., `*func_ptr = ...`) violates C language constraints and results in a compile-time error. The compiler strictly prevents direct modification of the executable text segment through function pointer assignment.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
