限制数组大小的目的是什么?
What is the purpose of restrict as size of array?
我明白restrict
是什么意思,但是我对这样的usage/syntax有点困惑:
#include <stdio.h>
char* foo(char s[restrict], int n)
{
printf("%s %d\n", s, n);
return NULL;
}
int main(void)
{
char *str = "hello foo";
foo(str, 1);
return 0;
}
编译成功 gcc main.c -Wall -Wextra -Werror -pedantic
限制在这种情况下如何工作并由编译器解释?
gcc 版本:5.4.0
In a function declaration, the keyword restrict may appear inside the square brackets that are used to declare an array type of a function parameter. It qualifies the pointer type to which the array type is transformed:
和示例:
void f(int m, int n, float a[restrict m][n], float b[restrict m][n]);
首先,
char* foo(char s[restrict], int n) { ....
与
相同
char* foo(char * restrict s, int n) {...
C11
章节 §6.7.6.2
允许语法
[...] The optional type qualifiers and the keyword static
shall appear only in a
declaration of a function parameter with an array type, and then only in the outermost
array type derivation.
这里restricted
的目的是提示编译器对于函数的每次调用,实际参数仅通过指针[=15]访问=].
我明白restrict
是什么意思,但是我对这样的usage/syntax有点困惑:
#include <stdio.h>
char* foo(char s[restrict], int n)
{
printf("%s %d\n", s, n);
return NULL;
}
int main(void)
{
char *str = "hello foo";
foo(str, 1);
return 0;
}
编译成功 gcc main.c -Wall -Wextra -Werror -pedantic
限制在这种情况下如何工作并由编译器解释?
gcc 版本:5.4.0
In a function declaration, the keyword restrict may appear inside the square brackets that are used to declare an array type of a function parameter. It qualifies the pointer type to which the array type is transformed:
和示例:
void f(int m, int n, float a[restrict m][n], float b[restrict m][n]);
首先,
char* foo(char s[restrict], int n) { ....
与
相同 char* foo(char * restrict s, int n) {...
C11
章节 §6.7.6.2
[...] The optional type qualifiers and the keyword
static
shall appear only in a declaration of a function parameter with an array type, and then only in the outermost array type derivation.
这里restricted
的目的是提示编译器对于函数的每次调用,实际参数仅通过指针[=15]访问=].