[todo0211]c语言指针,结构体的疑问

#include <stdio.h>
#include <mm_malloc.h>
struct ListNode {
    int val;
    struct ListNode *next;
};
struct ListNode* init(struct ListNode* L){//tt1
    /*我的实验结果告诉我:
    参数里面的L容器(tt1处)有了一个全新的位置,比如0x7ffee9ec28f8,而传入
    之前,L在main 里面(tt2处)的位置是: 0x7ffee9ec2928
    content 倒是和main 里面,声明L时候的 content 8 一样,所以tt3 输出8
    唯一不同的,就是address , tt4 就是 0x7ffee9ec28f8
    所以我们 洞见 一个 事实: call by value:
    tt1 的新L 把 main 里面的tt2 的L 的内容给拿了过来;
    所以或许有这么一个事实: ptrX = ptrY 的时候,ptrX 就是把prtY指向地址的
    内容给拿了过来。 一如 tt2 拿了 tt1 的content 8;

    实际上,我还是没理解 tt5,请回答,相关: 指向指针的指针
    */
    printf("content of node in init(bef), is: %d\n", L);//tt3 : 8
    printf("address of node in init(bef), is: %p\n", &L);//tt4
    L = (struct ListNode*) malloc (sizeof(struct ListNode*));
    printf("content of node in init(aft), is: %d\n", L);//guess not 8?enen
    printf("address of node in init(aft), is: %p\n", &L);//这里和tt4 的位置一样,为啥?malloc 没有分配新的空间?tt5
    printf("above should be different from in main one\n");
    L->val = 9;
    L->next = NULL;
    return L;
}
struct ListNode* fuc(struct ListNode* head)
{
    printf("address of node in fuc, is: %p\n", &head);//
    printf("val of node in fuc, is: %d\n", head->val);//9
    return NULL;
}
int main()
{
    struct ListNode* L = {8};//tt2
    printf("content of node in main(bef), is: %d\n", L);
    printf("address of node in main, is: %p\n", &L);//0x7ffee516c928
    L = init(L);
    fuc(L);
}

result:

posted on 2020-02-11 12:34  Paulkg12  阅读(122)  评论(0编辑  收藏  举报

导航