代码点读机 / C / restrict

restrict C · 关键字

告诉编译器该指针是访问它所指数据的唯一且独立的方式,便于优化。

怎么用

类型 *restrict 指针名;

小例子

#include <stdio.h>
void f(int *restrict a, int *restrict b) {
    *a = 1;
    *b = 2;
}
int main(void) {
    int x, y;
    f(&x, &y);
    printf("%d %d", x, y);
    return 0;
}

运行结果

输出 1 2

提醒:两个 restrict 指针指向同一对象、且一个修改另一个也访问时才是未定义行为;只读不写不算违规

官方文档:https://en.cppreference.com/w/c/language/restrict

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← C全部词条