简体   繁体   English

对 C 指针和 memory 地址有一些疑问

[英]Having some doubts about C pointers and memory address

I'm really newbie in C and I have this piece of code about LinkedList in C:我真的是 C 的新手,我在 C 有这段关于 LinkedList 的代码:

struct ListNode* addnode(struct ListNode *head,int val)
{
    struct ListNode* newnode = malloc(sizeof(struct ListNode));
    newnode->val = val;
    newnode->next = NULL;
    if (!head) head = newnode;
    else {
        struct ListNode *this = head;
        while(this->next)
            this = this->next;
        this->next = newnode;
    }
    return head;
}

int main(void)
{
    struct ListNode *head = NULL;

    head = addnode(head,4);
    addnode(head,9);
    addnode(head,1);

    return 0;
}

My question is: in the main why if I write我的问题是:主要是为什么我写

    addnode(head,4);
    addnode(head,9);
    addnode(head,1);

Instead of代替

head = addnode(head,4);
    addnode(head,9);
    addnode(head,1);

Doesn't work?不起作用? The first one is creating the following linkedlist: 4->9->1, and the second one is creating 3 different Linkedlist heads.第一个是创建以下链表:4->9->1,第二个是创建 3 个不同的链表头。 But wouldn't it be the same as we are always using the memory address of head?但是不就和我们一直使用head的memory地址一样吗? So the head is always saving his previous node.所以 head 总是在保存他之前的节点。

Thank you in advance I will appreciate your answers提前谢谢你,我会很感激你的回答

Initially the pointer head is initialized as a null pointer最初指针head被初始化为一个null指针

struct ListNode *head = NULL;

This pointer is passed by value to the function addnode该指针按值传递给 function addnode

addnode(head,4);

The function deals with a copy of the value of the pointer head declared in main that is used as an argument expression of the function. Changing the copy does not influence on the original pointer head . function 处理在 main 中声明的指针head值的副本,用作 function 的参数表达式。更改副本不会影响原始指针head

So after the call the pointer head will still be a null pointer.所以在调用之后指针head仍然是一个 null 指针。

The function returns the modified value of the copy of the pointer function 返回指针副本的修改值

struct ListNode* addnode(struct ListNode *head,int val)
{
    //... 
    if (!head) head = newnode;
    //...
    return head;
}

To change the original pointer head you need to assign the returned value to the pointer head like要更改原始指针head ,您需要将返回值分配给指针head ,如

head = addnode(head,4);

Now the pointer head points to the first node of the list.现在指针head指向列表的第一个节点。

All other calls of the function function 的所有其他电话

addnode(head,9);
addnode(head,1);

append new nodes to the tail of the list. append 个新节点到列表的尾部。 That is these calls do not influence on the value of the pointer that points to the first node of the list.也就是说,这些调用不会影响指向列表第一个节点的指针的值。 So there is no need to assign the returned value of the function to the pointer head though nevertheless you could write因此,无需将 function 的返回值分配给指针head ,尽管您可以编写

head = addnode(head,9);
head = addnode(head,1);

To make it more clear consider a very simple example为了更清楚地考虑一个非常简单的例子

#include <stdio.h>

int f( int x )
{
    x += 1;

    return x;
}

int main( void )
{
    x = 0;

    f( x );

    printf( "x = %d\n", x );
}

If you will not substitute this statement如果您不替换此声明

    f( x );

for为了

    x = f( x );

then the variable x will not be changed in main after calling the function f because the function f changes a copy of the value of the variable x used as the function argument.那么变量x在调用 function f后不会在 main 中更改,因为 function f更改了用作 function 参数的变量x值的副本。

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

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