#include <stdio.h>
int main(void) {
int (*str)[] = {"123456"};
printf("%p\n", str);
return 0;
}
这个代码会有警告$ gcc -g -Wall -o main main.c
main.c: In function ‘main’:
main.c:4:21: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
int (*str)[] = {"123456"};
^~~~~~~~
main.c:4:21: note: (near initialization for ‘str’)
$
等于号左边是一个指针,右边是一个初始化列表
#include <stdio.h>
int main(void) {
int (*str)[] = &(int *[]){"123456"};
printf("%p\n", str);
return 0;
}
改成这样还是有警告
$ gcc -g -Wall -o main main.c
main.c: In function ‘main’:
main.c:4:31: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
int (*str)[] = &(int *[]){"123456"};
^~~~~~~~
main.c:4:31: note: (near initialization for ‘(anonymous)[0]’)
main.c:4:20: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
int (*str)[] = &(int *[]){"123456"};
^
$
因为初始化列表中的是一个字符串,应该是下面这样的#include <stdio.h>
int main(void) {
const char *(*str)[] = &(const char *[]){"123456"};
printf("%p\n", str);
return 0;
}
$ gcc -g -Wall -o main main.c
$
|