以下是示例代码,而不是正常工作的代码。我只想知道*head和(*head)在C中指针方面的区别。
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;
}发布于 2013-06-06 17:29:22
*的优先级比->低,因此
*head->next将等同于
*(head->next)如果要取消引用head,则需要将取消引用运算符*放在括号内
(*head)->next发布于 2013-06-06 19:53:00
a+b和(a+b)之间没有区别,但a+b*c和(a+b)*c有很大的区别。这与*head和( *head ) ... (*head)->next使用*head的值作为指针,并访问它的下一个字段。*head->next相当于*(head->next) ...这在您的上下文中是无效的,因为head不是指向struct节点的指针。
发布于 2013-06-06 17:29:06
不同之处在于运算符precedence of C。
->的优先级比*高
执行
适用于 *head->next的
*head->next // head->next work first ... -> Precedence than *
^
*(head->next) // *(head->next) ... Dereference on result of head->next
^适用于 (*head)->next的
(*head)->next // *head work first... () Precedence
^
(*head)->next // (*head)->next ... Member selection via pointer *head
^https://stackoverflow.com/questions/16958390
复制相似问题