Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么gcc在类型定义指针的大小上出错_C - Fatal编程技术网

为什么gcc在类型定义指针的大小上出错

为什么gcc在类型定义指针的大小上出错,c,C,使用此代码 #include <stdio.h> #include <stdlib.h> struct nodetag {

使用此代码

#include <stdio.h>                                                                                  
#include <stdlib.h>                                                                                 
struct nodetag {                                                                                    
        struct nodetag *next;                                                                       
        struct nodetag *prev;                                                                       
        int a;                                                                                      
};                                                                                                  
typedef struct nodetag *node;                                                                       

int main(void)                                                                                      
{                                                                                                   
        node h;                                                                                     
        printf("%zu\n",sizeof(struct nodetag));                                                      
        printf("%zu\n",sizeof(*h));                                                                  
        printf("%zu\n",sizeof(*node));                                                               
}
为什么编译器在sizeof*节点上出错,而在sizeof*h上没有出错?

typedef为其他数据类型创建别名。也就是说,声明

typedef struct nodetag *node;
printf("%u\n",sizeof(*h));
将节点创建为struct nodetag*类型的别名。 此声明

sizeof(*node)

sizeof(*(struct node *))
无法取消引用类型,因此此语句会出错。您可以像在此语句中所做的那样取消对指针变量的引用

typedef struct nodetag *node;
printf("%u\n",sizeof(*h));
这是有效的,因为h是node类型,它是struct nodetag*类型的别名。取消对h的引用将给出struct nodetag

此外,sizeof运算符的结果类型为size\t。您应该使用%zu格式说明符而不是%u。

typedef为其他数据类型创建别名。也就是说,声明

typedef struct nodetag *node;
printf("%u\n",sizeof(*h));
将节点创建为struct nodetag*类型的别名。 此声明

sizeof(*node)

sizeof(*(struct node *))
无法取消引用类型,因此此语句会出错。您可以像在此语句中所做的那样取消对指针变量的引用

typedef struct nodetag *node;
printf("%u\n",sizeof(*h));
这是有效的,因为h是node类型,它是struct nodetag*类型的别名。取消对h的引用将给出struct nodetag


此外,sizeof运算符的结果类型为size\t。您应该使用%zu格式说明符而不是%u。

节点是类型,而不是指针。无法取消对它的引用。这与不能使用sizeof*int*的原因相同。另请参见:。sizeof的参数可以是类型或表达式。但是,不能将它们混合使用。node是一种类型,而不是指针。无法取消对它的引用。这与不能使用sizeof*int*的原因相同。另请参见:。sizeof的参数可以是类型或表达式。但你不能把它们混在一起。