我有这段代码:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void pointerOfPointer(struct node **reference)
{
struct node *temporary = malloc(sizeof(struct node));
temporary->data = 100;
temporary->next = 0;
printf("before: temporary->data %d\n", temporary->data);
temporary = *reference;
printf("after: temporary->data %d\n", temporary->data);
}
int main()
{
struct node *linkedlist = malloc(sizeof(struct node));
linkedlist->data = 15;
linkedlist->next = 0;
pointerOfPointer(&linkedlist);
return 0;
}
如何在不将*引用地址复制到临时局部变量的情况下访问pointerOfPointer函数中指向struct的指针?所以最后我可以使用operator - &gt;访问引用变量数据。直接,如reference-&gt; data?
答案 0 :(得分:3)
请记住,foo->bar
只是(*foo).bar
的语法糖。您要求的内容基本上是(**reference).data
,如果您愿意,可以将其重写为(*reference)->data
。