简体   繁体   English

如何将空指针(结构返回类型)更改为声明的结构?

[英]How to change null pointer (of struct return type) to declared struct?

I am new to C programming.我是 C 编程的新手。 I am trying to implement Linked list by myself.我正在尝试自己实现链接列表。 I am encountering problem with pointers我遇到了指针问题

I have function我有功能

void Insert(Node* head, int x)

to insert node at the beginning of Linked list.在链表的开头插入节点。 The problem is that when I insert the very first node and Node *head is NULL, the function Insert is not able to change the pointer address of null pointer to the newly created node.问题是,当我插入第一个节点并且 Node *head 为 NULL 时,函数 Insert 无法将空指针的指针地址更改为新创建的节点。 It seems as if the Node *head is passed by value and not by reference.似乎 Node *head 是按值而不是按引用传递的。 Code is provided below.下面提供了代码。 In order to debug how address is changed throughout the execution, I used printf function.为了调试整个执行过程中地址的变化方式,我使用了 printf 函数。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

int main() {
    Node *head = (Node*) malloc(sizeof(Node));
    head = NULL;

    printf("head in main(): %d\n", head); // For example: 100

    Insert(head, 25);
    return 0;
}

void Insert(Node *head, int x) {
    Node *temp = (Node*) malloc(sizeof(Node));
    temp->data = x;
    temp->next = head;

    printf("temp->next address: %d\n", temp->next); // also 100

    head = temp;

    printf("%d\n", head); // not 100, something else i.e  200
}

It seems as if the Node *head is passed by value and not by reference.似乎 Node *head 是按值而不是按引用传递的。

That is exactly right -- in C every parameter is always passed by value.这是完全正确的——在 C 中,每个参数总是按值传递。 The pointer is a value, and that value is passed by value, and the call指针是一个值,该值是按值传递的,调用

Insert(head, 25);

can never change the value of the variable named head .永远不能改变名为head的变量的 It reads the value of the variable (this value is a null pointer), gives that value to the function and never touches the variable head again no matter what the function does.读取变量的值(该值是一个空指针),将该值提供给函数并且无论函数做什么都不会再次接触变量head

(Note that in your program you have two variables that are both named head -- one in main() and the other in Insert() . The variable in Insert() silently disappears when the function returns; nothing will automatically try to copy its value to the similarly-named variable in main() ). (请注意,在你的程序有两个变量都命名为head -一个在main()另一个在Insert()中的变量Insert()悄无声息消失当函数返回时,没有什么会自动尝试复制其main()同名变量的值)。

If you want to (conceptually) pass head by reference , you need to actually pass a pointer to it -- that is, in this case, a pointer to a pointer!如果你想(从概念上)通过引用传递head ,你需要实际传递一个指向它的指针——也就是说,在这种情况下,一个指向指针的指针! You'd need to declare your function as你需要将你的函数声明为

void Insert(Node **head, int x) { ... }

and call it as并将其称为

Insert(&head, 25);

Then the actual parameter is a pointer to the variable head which gives the function a chance to update that variable, if you deference the parameter where appropriate:然后实际参数是指向变量head的指针,如果您在适当的情况下尊重参数,则该指针使函数有机会更新该变量:

// ...
temp->next = *head;
// ...
*head = temp;
// ...

Pass a pointer to a pointer to head.将指针传递给指向 head 的指针。 That way, you can set head to null.这样,您可以将 head 设置为 null。

void Insert(Node **head, int x) {

    ...

    if (*head == NULL) {
        *head = temp;
    }

    else {
        ...
        *head->next = temp;
    }
}

Usage:用法:

Node *head = NULL;
Insert(&head, 10);

Having three answers suggesting the same I would like to offer an alternative: Instead of passing a Node ** to Insert() you could instead have it return the new head , thus:有三个建议相同的答案,我想提供一个替代方案:不是将Node **传递给Insert()你可以让它返回新的head ,因此:

Node *Insert( Node *head, int x )
{
     ... your code ...
     return head;
}

and if you call it by如果你叫它

head = Insert( head, 24 );

That is neither better nor worse then the other solution so you my do whatever you prefer这既不比另一个解决方案更好也不差,所以你我做任何你喜欢的

There are number of issues here.这里有很多问题。
1. Your printf statements need to be corrected. 1. 你的 printf 语句需要更正。
2. To insert function you can pass double pointer. 2. 插入函数可以传递双指针。
3. Inside main function, you need not to do Node *head = (Node*) malloc(sizeof(Node)); 3.在main函数里面,不需要做Node *head = (Node*) malloc(sizeof(Node));

I have modified your code as shown below.我已经修改了您的代码,如下所示。 You can try running it and co-relate above points.您可以尝试运行它并关联以上几点。

typedef struct Node {
    int data;
    struct Node *next;
} Node;

void Insert(Node **head, int x);

void Insert(Node **head, int x) {
    Node *temp = (Node*) malloc(sizeof(Node));
    temp->data = x;
    temp->next = *head;

    printf("temp->next address: %d\n", temp->data); // also 100

    *head = temp;

    printf("%d\n", (*head)->data); // not 100, something else i.e  200
}

int main() {
    Node *head = NULL;
    head = NULL;

    Insert(&head, 25);
    printf("head in main(): %d\n", head->data); // For example: 100
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM