我是入门编程课程的助教,有些学生犯了这种错误:
char name[20];
scanf("%s",&name);
Run Code Online (Sandbox Code Playgroud)
这并不奇怪,因为他们正在学习......令人惊讶的是,除了gcc警告之外,代码也起作用(至少这部分).我一直在努力理解并编写了以下代码:
void foo(int *v1, int *v2) {
if (v1 == v2)
printf("Both pointers are the same\n");
else
printf("They are not the same\n");
}
int main() {
int test[50];
foo(&test, test);
if (&test == test)
printf("Both pointers are the same\n");
else
printf("They are not the same\n");
}
Run Code Online (Sandbox Code Playgroud)
编译和执行:
$ gcc test.c -g
test.c: In function ‘main’:
test.c:12: warning: passing argument 1 of ‘foo’ from incompatible pointer type
test.c:13: warning: comparison of distinct pointer types lacks a cast …
Run Code Online (Sandbox Code Playgroud) 在 stackoverflow 上的其他地方(例如,此处,不幸的是,目前接受的答案是不正确的——但至少最高投票的答案是正确的),C 标准规定,在几乎所有情况下,数组char my_array[50]
都将隐式转换为char *
when它被使用,例如通过传递给一个函数 as do_something(my_array)
,给定一个声明void do_something(char *stuff) {}
。也就是说,代码
void do_something(char *my_array) {
// Do something
}
void do_something_2(char my_array[50]) {
// Do something
}
int main() {
char my_array[50];
do_something(my_array);
do_something_2(my_array);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由 gcc 编译,没有任何严格级别的警告。
然而,C11的第6.3.2.1.3规定,此转换不发生特别是如果一个写操作&my_array
,或sizeof(my_array)
(而且这些是唯一次时,不会发生这种转换)。后一条规则的目的对我来说很明显——sizeof
数组等于指向第一个元素的指针的大小是非常混乱的,所以应该避免。
但是这条规则的第一部分的目的(与写作有关&my_array
)完全没有让我明白。看,该规则使类型为&my_array
(在 C 标准的表示法中)char (*)[50]
,而不是char *
。这种行为什么时候有任何用处?确实,除了 - 目的之外sizeof
,为什么该类型根本char (*)[50]
存在?
例如,在 stackexchange(例如 …