我正在使用 gcc 4.8.1,但我无法理解以下程序的输出。
#include<stdio.h>
int main()
{
char* a, b, c;
int* d, e, f;
float* g, h, i;
printf("Size of a %zu and b %zu and c %zu \n", sizeof(a), sizeof(b), sizeof(c));
printf("Size of d %zu and e %zu and f %zu and int is %zu \n", sizeof(d), sizeof(e), sizeof(f), sizeof(int*));
printf("Size of g %zu and h %zu and i %zu and float is %zu \n", sizeof(g), sizeof(h), sizeof(i), sizeof(float));
return 0;
}
输出为
Size of a 4 and b 1 and c 1
Size of d 4 and e 4 and f 4 and int is 4
Size of g 4 and h 4 and i 4 and float is 4
我的问题是为什么 b
和 c
不是 char*
类型,而 int
是可能的> 和 float
。我想知道 C 语法如何拆分声明。
最佳答案
在这样的声明中
char* a, b, c;
只有类型char
用于所有变量,而不是是否为指针(*
符号)。当这样使用时,这个(等效的)语法使它更清晰:
char *a, b, c;
定义3个指针:
char *a, *b, *c;
或者在经常使用多个指向 char 的指针的情况下,也许做一个 typedef:
typedef char* char_buffer;
char_buffer a, b, c;
关于c - c中指针的声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27462503/