C 限制结构内的关键字和指针

C 限制结构内的关键字和指针,c,function,struct,restrict-qualifier,C,Function,Struct,Restrict Qualifier,使用restrict关键字,如下所示: int f(int* restrict a, int* restrict b); 我可以指示编译器数组a和b不重叠。假设我有一个结构: struct s{ (...) int* ip; }; 并编写一个函数,该函数接受两个结构对象: int f2(struct s a, struct s b); 在这种情况下,我如何类似地指示编译器a.ip和b.ip不重叠?您也可以在结构内部使用restrict struct s { /* ... */

使用
restrict
关键字,如下所示:

int f(int* restrict a, int* restrict b);
我可以指示编译器数组a和b不重叠。假设我有一个结构:

struct s{
(...)
int* ip;
};
并编写一个函数,该函数接受两个
结构
对象:

int f2(struct s a, struct s b);

在这种情况下,我如何类似地指示编译器
a.ip
b.ip
不重叠?

您也可以在结构内部使用
restrict

struct s {
    /* ... */
    int * restrict ip;
};

int f2(struct s a, struct s b)
{
    /* ... */
}

因此,编译器可以假设
a.ip
b.ip
用于在每次调用
f2
函数期间引用不相交的对象。

检查此指针示例,您可能会得到一些帮助

// xa and xb pointers cannot overlap ie. point to same memmpry location.
void function (restrict int *xa, restrict int *xb)
{
    int temp = *xa;
    *xa = *xb;
    xb = temp;
}
如果两个指针声明为restrict,那么这两个指针不会重叠

已编辑


我看不出这是如何回答这个问题的-OP清楚地知道如何使用普通指针,代码在问题中。也许编写一个与a.ip和b.ip类型而不是a和b类型一起工作的函数是一个好的解决方案。这取决于结构的性质,如果a和b是OO设计中使用的不完整类型,那么该方法将无法工作。