gpt4 book ai didi

c - 遍历字符串的结构链表(C编程)

转载 作者:太空宇宙 更新时间:2023-11-04 01:30:37 26 4
gpt4 key购买 nike

如果C中的结构定义为

struct StringList{ char* value; struct StringList* next; };

我必须从头开始打印每个元素的值地址

所以我有:

void print (struct StringList* head){ 

struct StringList* sp = Head;
while ((sp->next)->next != NULL){
printf( "value: %d", &sp->value);
}

我在程序员交流上发帖也不知道哪个网站更合适

最佳答案

您不需要额外的 sp 变量。你也没有在 while 循环内更新它。还有

(sp->next)->next!=NULL

如果 sp->next 为 NULL,将会崩溃。我正在展示一种通过重用您的 head 变量的方法。

void print (struct StringList* head){ 
while (head != NULL){
printf( "value: %p", &(head->value));
head = head->next;
}
}

关于c - 遍历字符串的结构链表(C编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23123435/

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