C restrict Keyword
Example
Use restrict on two pointer parameters:
void addTo(int* restrict total, int* restrict num) {
*total += *num;
}
int main() {
int x = 10;
int y = 5;
addTo(&x, &y);
printf("%d", x);
return 0;
}
Definition and Usage
The restrict keyword is used on a pointer. It is a promise to the compiler that only this pointer is used to access the data it points to.
With this promise, the compiler can make the code faster. It is mostly used on function parameters. For example, the standard memcpy() function uses it.
If you break the promise, for example with addTo(&x, &x), the result is undefined (the program can behave in unexpected ways).
Related Pages
Read more about pointers in our C Pointers Tutorial.