如何将箭头指针传递给函数?

struct node { int x; struct node *next; }; void allocateMemory(struct node *some_node) { some_node = malloc(sizeof(struct node)); } 

在另一个function:

 struct node add(struct node *root, struct node *thisNode, int value) 

我试着这样说:

 allocateMemory(thisNode->next); 

我收到运行时错误。 它什么都不做。 然而,当我在上述函数中执行与allocateMemory()相同的操作时,即:

 thisNode->next = malloc(sizeof(struct node)); 

它做了它应该做的事情。 我究竟做错了什么?

你需要paas 指针指针

当你有指针时,你可以改变指针指向的值,当你想改变实际的指针时,你需要更深入一步。

函数add也不应该返回值而是指针?

 struct node { int x; struct node *next; }; void allocateMemory(struct node **some_node) { *some_node = (struct node*)malloc(sizeof(struct node)); } struct node* add(struct node *root, struct node *thisNode, int value) { allocateMemory(&thisNode->next); thisNode->x = value; root->next = thisNode; return thisNode; } 

这段代码在这里:

 void allocateMemory(struct node *some_node) { some_node = malloc(sizeof(struct node)); } 

你可以写 :

 void allocateMemory(struct node **some_node) { *some_node = malloc(sizeof(struct node)); } 

在打电话的时候:

 allocateMemory(&thisNode->next);