`restrict` — C Keyword

`restrict` — C Keyword

The restrict keyword in C99: asserts that a pointer is the only alias to its target, enabling better optimization.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

restrict (C)

A type qualifier for pointers. Asserts that the pointer is the sole access path to the object it points to (no aliasing). Allows the compiler to produce more efficient code.

Syntax

Type* restrict ptr;
return-type func(Type* restrict a, Type* restrict b);

Example

#include <stdio.h>

/* Without restrict, the compiler must assume a and out might alias */
void add_arrays(const float* restrict a,
                const float* restrict b,
                float*       restrict out,
                int n) {
    for (int i = 0; i < n; ++i) {
        out[i] = a[i] + b[i];  /* compiler may now vectorize freely */
    }
}

int main(void) {
    float a[]   = {1.0f, 2.0f, 3.0f};
    float b[]   = {4.0f, 5.0f, 6.0f};
    float out[3];

    add_arrays(a, b, out, 3);
    for (int i = 0; i < 3; ++i) {
        printf("%.0f ", out[i]);  /* 5 7 9 */
    }
    printf("\n");
    return 0;
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}