更改指针值时出现问题

时间:2017-04-17 03:01:43

标签: c

所以我今天开始学习C并且通过辅导练习没有任何问题,但我终于被卡住了。我检查了stackoverflow,其他reddit帖子和youtube视频。我正在尝试创建一个链表。下面是我的代码,其中详细说明了我认为代码正在做什么。当我在CLion中运行它时,printList函数没有输出。但是,如果我取消注释被删除的行(其中只有3行,很容易找到)并且我注释掉我对push()的调用,printList函数将打印1,就像它应该的那样。据我所知,3个注释行和push()中的行是做同样的事情,为什么输出不同?

#include <stdio.h>
#include <malloc.h>

typedef struct node {
    int val;
    struct node *next;
} node_t; //node struct is defined

void printList(node_t *head); //printlist function is initialized, accepts a node_t pointer (correct vocabulary?)
void push(node_t *head, int val); //push function is initialized

int main() {
    node_t *head = NULL; //a pointer is created that points to a struct of type node_t, and currently points to NULL
    push(head, 1); //push function accepts node_t pointer which currently points to NULL, and an int: 1
//    head = (node_t *) malloc(sizeof(node_t)); //the pointer "head" now points to a section of memory that can
    //hold a node_t struct
//    head->val = 1; //head's "val" variable now points to the int 1
//    head->next = NULL; // head's "next" variable now points to NULL
    printList(head);
    return 0;
}

void printList(node_t *head) {
    node_t *current = head;
    while (current != NULL) {
        printf("%d ", current->val);
        current = current->next;
    }
}

void push(node_t *head, int val) {
    node_t *current = head; //the pointer "current" now points to the value that head pointed to (NULL)
    current = (node_t *) malloc(sizeof(node_t)); //just enough memory is allocated for a node_t struct,
    // and the variable current now points to it
    current->val = val; //current's "val" variable now points to the int "val" from the function parameters
    current->next = NULL; //current's "next" variable, which is a node_t pointer, now points to NULL

}

2 个答案:

答案 0 :(得分:3)

继续发表评论,如果您要使用void功能而不是返回node_t *并分配到head,则需要传递地址 headpush?为什么?当您在current中分配push时,它有自己的地址,与head中的main无关。

当您指定node_t *current = head;时,您正在为current分配头部(不是头部本身)。现在,副本初始化为NULL,但head中指针push的地址head中的<{em>}不同main 1}}。

你传递给push的是什么? (ans:head中的NULL副本,例如head指针} pushhead中有一个独立且不同的指针地址 mainpush中的1}}。当push返回时,您没有引用headcurrentcurrent)中的任何一个指针,因为push是在head内宣布的当head作为参数传递时,编译器创建的推送中head副本的地址也已消失。要使push中的headmain中的push具有相同的地址,请将其地址传递给int main (void) { node_t *head = NULL; push (&head, 1); printList (head); putchar ('\n'); /* tidy up */ return 0; } ... void push(node_t **head, int val) { node_t *current = malloc(sizeof *current); current->val = val; current->next = NULL; *head = current; } ,例如

node_t *

如果您还有其他问题,请与我们联系。

您还可以返回指针(类型main)并在int main (void) { node_t *head = NULL; head = push (1); printList (head); putchar ('\n'); /* tidy up */ return 0; } ... node_t *push(int val) { node_t *current = malloc(sizeof *current); current->val = val; current->next = NULL; return current; } 中分配,例如

{{1}}

答案 1 :(得分:0)

关键问题是您按值而不是地址传递指针。因此,当调用者退出时,指针(头部)的值根本不会改变!

您可以像这样更改push

void push(node_t **head, int val) {
(*head) = (node_t *) malloc(sizeof(node_t)); //just enough memory is allocated for a node_t struct,
(*head)->val = val; //current's "val" variable now points to the int "val" from the function parameters
(*head)->next = NULL; //current's "next" variable, which is a node_t pointer, now points to NULL
}

然后您可以这样称呼它:push(&head, 1);

在此示例中,我按地址传递head并通过取消引用更新push函数中的值。因此可以从push函数中看出它的变化。

希望它有所帮助!

(我在stackoverflow上的第一个答案:P)