gpt4 book ai didi

c - while循环不起作用?

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

几天前我们在类里面写了这段代码,老师试图解释它,我们大多数人都听不懂。我现在几乎完全理解它,但为什么第二个 while in main 不起作用?它应该在弹出之前输出一个名称。

#include <stdio.h>
#include<stdlib.h>

typedef struct STK_S{
char name[100];
struct STK_S *next;
}STK;
int push(STK **ppS, STK *pD);
int pop(STK **ppS, STK *pD);

int main(){
STK *pS, d;
pS = NULL;
while (1){
printf_s("Ime ");
gets_s(d.name, 100);
if (d.name[0] == 0)
break;
push(&pS, &d);
}
while (pop(&pS, &d))
printf_s("\n%s", d.name);
return 0;
}

int push(STK **ppS, STK *pD){
STK *pt;
pt = (STK *)malloc(sizeof(STK));
if (pt == NULL)
return 0;
*pt = *pD;
pt->next = *ppS;
*ppS = pt;
return 1;
}

int pop(STK **ppS, STK *pD){
STK *pt;
if (*ppS == NULL){
printf("Empty stack.\n");
return NULL;
}
*pD = **ppS;
pt = *ppS;
*ppS = pt->next;
free(pt);
return 0;
}

最佳答案

pop 返回 NULL0,它们都转换为 false。所以循环不会运行一次。

关于c - while循环不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50317373/

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