gpt4 book ai didi

c - 为什么这2个指针地址没有区别?

转载 作者:行者123 更新时间:2023-11-30 20:25:08 26 4
gpt4 key购买 nike

我有这个结构:

typedef struct lin_list{
char* payload;
struct lin_list* next;
}LinListCell, *LinList_p;

这是我的主要内容,包含向您展示我的问题所需的信息:

int main(void){
LinList_p linlistpointer;

char payLoad[80+1];
strcpy(payLoad,"Test");

printf("%X\n\n",linlistpointer);

linlistpointer = LinListInsertFirst(linlistpointer,LinListAllocCell(payLoad));
linlistpointer = LinListInsertFirst(linlistpointer,LinListAllocCell(payLoad));
linlistpointer = LinListInsertFirst(linlistpointer,LinListAllocCell(payLoad));
listLists(linlistpointer);
linlistpointer = LinListExtractFirst(linlistpointer);
}

这些是我的 main 中的函数。

LinList_p LinListInsertFirst(LinList_p *anchor, LinList_p newcell){
newcell->next=anchor;
anchor = newcell;

printf("\nNew first Element: %X\n\n",anchor);

printf("\nAnchor: %X\n\n",anchor);
return anchor;
}

LinList_p LinListAllocCell(char* payload){
LinList_p listpointer;
listpointer = malloc(sizeof(LinListCell));
listpointer->payload = strdup(payload);
listpointer->next = NULL;

printf("New Cell:\nAdress: %X\nPayload: %s\n",listpointer, listpointer->payload);
return listpointer;
}

void listLists(LinList_p pointer){

int counter=1;
LinList_p thispointer = pointer;

printf("Listlistspointer: %X",pointer);
printf("\n\n----------------");
while(thispointer){
printf("\nNow: #%d\n",counter);
printf("Adresse: %X\n",thispointer);
printf("Next: %X\n",thispointer->next);
printf("Payload: %s\n",thispointer->payload);
printf("----------------");
thispointer=thispointer->next;
counter++;
}
printf("\n\n");
}

LinList_p LinListExtractFirst(LinList_p *anchor){

printf("%X",anchor);

return NULL;

}

请注意,LinListExtractFirst 到目前为止尚未完成,实际上我只是想检查我的指针是否有效并卡在那里,最终到这里。

如您所见,一个被赋予一个 LinList_p*,另一个被赋予一个简单的 LinList_p。

但是,当我尝试通过打印出这两个值时

printf("\nAdress: %X\n\n",pointer);

在这两个函数中,我在两个函数中得到完全相同的值,这与我在主函数中得到的地址完全相同。

谁能给我解释一下吗?两者似乎是相同的,但是我无法通过访问结构变量

pointer->variable;

在给定 LinList_p* 的函数中,因此必须存在差异。

我现在很困惑......

最佳答案

linlistpointer 是一个未初始化的指针。它的值是未定义的,依赖它的值是未定义的行为。之后的一切都无关紧要,因为您的程序几乎可以做任何事情,并且从编译器的角度来看都被认为很好。

关于c - 为什么这2个指针地址没有区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30486692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com