简体   繁体   English

C 数据结构,变量

[英]C data structure, variables

Sorry for noob question对不起菜鸟问题

I have a struct:我有一个结构:

typedef struct  s_test
{
    int         head;
    char        *end;
}               t_test;

My code:我的代码:

int main()
{
   t_test a;
   t_test *b;
}

What is different between a.head and b->head ?. a.headb->head什么不同? Why can't I type a->head or b.head ?.为什么我不能输入a->headb.head ?。 I know b is a pointer which holds an address of a variable type t_test .我知道b是一个指针,它包含一个变量类型t_test的地址。

Doing b->head is just convenient syntactic sugar for doing (*b).head .b->head只是做(*b).head方便语法糖。

You have to dereference a pointer to a struct before you can use the dot notation on it, that's why you cannot do b.head .您必须先取消引用指向结构的指针,然后才能在其上使用点表示法,这就是您不能执行b.head的原因。

The reason why you cannot do a->head is that a is not a pointer, so it cannot be dereferenced.你不能做a->heada不是指针,所以它不能被取消引用。 The dereferencing operator * only makes sense for pointers.解引用运算符*仅对指针有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM