[英]What is the right way for modifying a pointer within a struct in a function?Am I doing it wrong How to do it?
I have struct I defined it as node1, Now in the main()
function created 3 pointers to node node1 node2 which are pointers to the type node1(which is the struct node i created).我有 struct 我将其定义为 node1,现在在
main()
function 创建了 3 个指向节点 node1 node2 的指针,它们是指向 node1 类型的指针(这是我创建的结构节点)。
In this first piece of code i got out output在这第一段代码中,我得到了 output
3
9
2
This means that when I change the node->next
inside the function it is not itself getting reflected in the main function it works only inside the function although I here used call by reference(Next piece of code)这意味着当我在 function 中更改
node->next
时,它本身并没有反映在主 function 中,它仅在 function 中起作用
Am I doing it wrong?我做错了吗?
Code代码
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void func(struct Node* node) {
node->data = 9;
node->next = (node->next)->next;
printf("%d\n", (node->next)->data);
}
int main() {
struct Node *node0, *node1, *node2;
node0 = (struct Node *)malloc(sizeof(struct Node));
node1 = (struct Node *)malloc(sizeof(struct Node));
node2 = (struct Node *)malloc(sizeof(struct Node));
node0->data = 1;
node0->next = node1;
node1->data = 2;
node1->next = node2;
node2->data = 3;
node2->next = NULL;
func(node0);
printf("%d\n%d", node0->data, node1->data);
}
Now this is the code that i wrote first with the call by reference this ne also gives the same output现在这是我通过引用调用首先编写的代码,这个 ne 也给出了相同的 output
my desired output is我想要的 output 是
3
9
3
So even with the call by reference its not working what's the solution for this?因此,即使通过引用调用它也不起作用,解决方案是什么?
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void func(struct Node **node) {
(*node)->data = 9;
((*node)->next) = (((*node)->next)->next);
printf("%d\n", ((*node)->next)->data);
}
int main() {
struct Node *node0, *node1, *node2;
node0 = (struct Node *)malloc(sizeof(struct Node));
node1 = (struct Node *)malloc(sizeof(struct Node));
node2 = (struct Node *)malloc(sizeof(struct Node));
node0->data = 1;
node0->next = node1;
node1->data = 2;
node1->next = node2;
node2->data = 3;
node2->next = NULL;
func(&node0);
printf("%d\n%d", node0->data, node1->data);
}
What is the right way to do this?这样做的正确方法是什么?
I want a solution to modify the pointer inside the struct without returning and reassigning.我想要一个解决方案来修改结构内的指针而不返回和重新分配。
So Here's what i want.所以这就是我想要的。 After the function call i want node1 variable to point to the exact location as node2.
在 function 调用之后,我希望 node1 变量指向 node2 的确切位置。
I have struct I defined it as node1, Now in the main()
function created 3 pointers to node node1 node2 which are pointers to the type node1(which is the struct node i created).我有 struct 我将其定义为 node1,现在在
main()
function 创建了 3 个指向节点 node1 node2 的指针,它们是指向 node1 类型的指针(这是我创建的结构节点)。
In this first piece of code i got out output在这第一段代码中,我得到了 output
3
9
2
This means that when I change the node->next
inside the function it is not itself getting reflected in the main function it works only inside the function although I here used call by reference(Next piece of code)这意味着当我在 function 中更改
node->next
时,它本身并没有反映在主 function 中,它仅在 function 中起作用
Am I doing it wrong?我做错了吗?
Code代码
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void func(struct Node* node) {
node->data = 9;
node->next = (node->next)->next;
printf("%d\n", (node->next)->data);
}
int main() {
struct Node *node0, *node1, *node2;
node0 = (struct Node *)malloc(sizeof(struct Node));
node1 = (struct Node *)malloc(sizeof(struct Node));
node2 = (struct Node *)malloc(sizeof(struct Node));
node0->data = 1;
node0->next = node1;
node1->data = 2;
node1->next = node2;
node2->data = 3;
node2->next = NULL;
func(node0);
printf("%d\n%d", node0->data, node1->data);
}
Now this is the code that i wrote first with the call by reference this ne also gives the same output现在这是我通过引用调用首先编写的代码,这个 ne 也给出了相同的 output
my desired output is我想要的 output 是
3
9
3
So even with the call by reference its not working what's the solution for this?因此,即使通过引用调用它也不起作用,解决方案是什么?
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void func(struct Node **node) {
(*node)->data = 9;
((*node)->next) = (((*node)->next)->next);
printf("%d\n", ((*node)->next)->data);
}
int main() {
struct Node *node0, *node1, *node2;
node0 = (struct Node *)malloc(sizeof(struct Node));
node1 = (struct Node *)malloc(sizeof(struct Node));
node2 = (struct Node *)malloc(sizeof(struct Node));
node0->data = 1;
node0->next = node1;
node1->data = 2;
node1->next = node2;
node2->data = 3;
node2->next = NULL;
func(&node0);
printf("%d\n%d", node0->data, node1->data);
}
What is the right way to do this?这样做的正确方法是什么?
I want a solution to modify the pointer inside the struct without returning and reassigning.我想要一个解决方案来修改结构内的指针而不返回和重新分配。
So Here's what i want.所以这就是我想要的。 After the function call i want node1 variable to point to the exact location as node2.
在 function 调用之后,我希望 node1 变量指向 node2 的确切位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.