C中*head和(*head)指针之间的差异
下面是一个示例代码,不是工作代码。 我只想知道C中指针中C中*head和(*head)指针之间的差异,c,pointers,head,C,Pointers,Head,下面是一个示例代码,不是工作代码。 我只想知道C中指针中*head和(*head)之间的区别 int insert(struct node **head, int data) { if(*head == NULL) { *head = malloc(sizeof(struct node)); // what is the difference between (*head)->next and *head->next ?
*head
和(*head)
之间的区别
int insert(struct node **head, int data) {
if(*head == NULL) {
*head = malloc(sizeof(struct node));
// what is the difference between (*head)->next and *head->next ?
(*head)->next = NULL;
(*head)->data = data;
}
这种差异是由运算符引起的
->
的优先级高于*
执行 对于
*head->next
*head->next // head->next work first ... -> Precedence than *
^
*(head->next) // *(head->next) ... Dereference on result of head->next
^
(*head)->next // *head work first... () Precedence
^
(*head)->next // (*head)->next ... Member selection via pointer *head
^
对于
(*head)->下一步
*head->next // head->next work first ... -> Precedence than *
^
*(head->next) // *(head->next) ... Dereference on result of head->next
^
(*head)->next // *head work first... () Precedence
^
(*head)->next // (*head)->next ... Member selection via pointer *head
^
->
有超过*
(取消引用)运算符,因此需要在*头周围插入括号()
,以覆盖优先级。所以正确的是(*head)->next
,因为head
是指向结构指针的指针
*head->next
与*(head->next)
相同,这是错误的(给你编译器错误,实际上语法错误)*
的优先级低于->
,所以
*head->next
相当于
*(head->next)
如果要取消引用head
,需要将取消引用操作符*
放在括号内
(*head)->next
没有区别
通常
然而,在本例中,它用于克服运算符优先级的问题:->
比*
绑定得更紧密,因此没有括号*head->next
就相当于*(head->next)
由于这不是所希望的,因此括号中插入了*head
,以便按正确的顺序进行操作。a+b和(a+b)之间没有区别,但a+b*c和(a+b)*c之间有很大区别。和*头和(*头)一样。。。(*head)->next使用*head的值作为指针,并访问其下一个字段*head->next相当于*(head->next)。。。在您的上下文中无效,因为head不是指向结构节点的指针。*(head)->下一步?(*头)->下一个@谢谢,那是一个相当重要/尴尬的打字错误!嘿,还有head->next和*head->next之间有什么区别?两者都不同,因为head->next
不会被取消引用。和*头->下一步
像->*(头->下一步)
一样工作,所以总是使用()
来确保正确执行。@rahurleddy:你明白了吗?请随意提问。(*head)->next和head->next@RahulReddy(*head)->next
是正确的,其中adhead->next
是错误的,语法错误,因为head是指向指针的指针,并且没有next作为成员。