[英]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.head
和b->head
什么不同? Why can't I type a->head
or b.head
?.为什么我不能输入a->head
或b.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->head
是a
不是指针,所以它不能被取消引用。 The dereferencing operator *
only makes sense for pointers.解引用运算符*
仅对指针有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.