简体   繁体   English

在 function 的结构中修改指针的正确方法是什么?我做错了怎么办?

[英]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.

相关问题 向右移动/我在做什么错? - shifting right / what I am doing wrong? 在C#中用struct调用C函数-我在做什么错? - Calling C function with struct in C# - what am I doing wrong? 在 C 中传递结构体时我做错了什么? - What am I doing wrong in passing a struct around in C? malloc 和结构数组的重新分配我做错了什么? - What am I doing wrong with malloc and realloc of array of struct? 结构不存储数据? 不知道我做错了什么 - The struct is not storing data? Not sure what I am doing wrong C中的指针问题 - 我在这里做错了什么? - Pointer issue in C - what am I doing wrong here? 我这样做对吗? 将结构体中的指针映射到结构体外部的位置,以使用IPC共享内存 - Am I doing this right? Mapping pointer in struct to position outside of struct for IPC shared memory usage 我在这里做错了什么? 无论我做什么,function askSubscriptionQuestions 都不会存储 4 个浮点数的值 - What am I doing wrong here? No matter what I do, the function askSubscriptionQuestions does not store the values of the 4 floats 在C中创建和初始化包含函数指针的结构的正确方法是什么? - What is the right way of creating and initializing a struct containing a function pointer in C? 使用结构将链表传递给 function 我做错了什么? - Pass linked list to function with struct what I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM