c - c中指针的声明

标签 c char int

我正在使用 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

我的问题是为什么 bc 不是 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/

相关文章:

c++ - 类型 "const char *"的参数与类型 "char *"的参数不兼容

c++ - 字符数组问题

android - 如何在 surfaceview 类中保存分数/高分(int)?

c - 我怎样才能让这个程序更快?

c - 如何检查 int var 是否包含特定数字

c - 套接字,为什么 ip 以整数格式发送?

java - 缓冲写入器在文本文件上写入内存垃圾

python - 将整数转换为具有特定格式的十六进制字符串

c - 通过在 C 中标记字符串来尝试读取文本文件时出现无限循环

c - 初始化具有 Union 的 Struct 数组的元素时出现问题