gpt4 book ai didi

c++ - C丢失指针

转载 作者:行者123 更新时间:2023-11-30 21:37:43 25 4
gpt4 key购买 nike

当我使用“1”命令插入一个新的结构体 A 时(因此我已经有一个或多个结构体 A 链接到一个或多个结构体 S),我丢失了前一个结构体 A 与其结构体 S 的链接。例如:

command 1 and then command 3 : 2014 and command 4

Output:

year:2014

matricola:1

now command 1 and then command 3 : 2015 and command 4

Output:

year:2015

matricola:2

year:2014

no S struct

希望这个例子对你有帮助这是代码:

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

struct S
{
int matr;
struct S* next;
};
struct A
{
int year;
struct A *nextA;
struct S *nextS;
};
int years = 2013;
int matricola=0;
void insA(struct A**T);
void printA(struct A*T);
void insS(struct A **T);
void printS(struct A *T);

int main()
{
struct A *T=NULL;
int cmd,sc=0;

while(1)
{
printf("\n command:");
sc=scanf("%d",&cmd);
while(sc==0)
{
printf("\nerror:");
fflush(stdin);
sc=scanf("%d",&cmd);
}
if(cmd==1)
{
insA(&T);
}
if(cmd==2)
{
printA(T);
}
if(cmd==3)
{
insS(&T);
}
if(cmd==4)
{
printS(T);
}
}
return 0;
}
void insA(struct A**T)
{
struct A *new_p=(struct A*)malloc(sizeof(struct A));
if(new_p==NULL)
{
printf("Errore");
exit(0);
}
years++;
new_p->nextA=NULL;
new_p->nextS=NULL;

if((*T)==NULL)
{
(*T)=new_p;
}
else
{
new_p->nextA=(*T);
(*T)=new_p;
}
new_p->year=years;
}
void printA(struct A *T)
{
struct A *tmp=T;
while(tmp!=NULL)
{
printf("\n%d",tmp->year);
tmp=tmp->nextA;
}
return;
}
void insS(struct A **T)
{
int search,sc=0;
struct S* new_p=(struct S*)malloc(sizeof(struct S));
if(new_p==NULL)
{
printf("error");
exit(0);
}
new_p->next=NULL;

printf("\nyear in which to insert:");
sc=scanf("%4d",&search);
while(sc==0 || search > years || search <= 2013)
{
printf("\nerror:");
fflush(stdin);
sc=scanf("%4d",&search);
}

struct A*tmp=*T;
while(tmp!=NULL)
{
if(tmp->year==search)
{
matricola++;
if(tmp->nextS==NULL)
{
tmp->nextS=new_p;
}
else
{
new_p->next=tmp->nextS;
tmp->nextS=new_p;
}
}
tmp=tmp->nextA;
}
new_p->matr=matricola;
return;
}
void printS(struct A *T)
{
struct A *tmp=T;
struct S *s=tmp->nextS;
while(tmp!=NULL)
{
printf("\nyear:%d",tmp->year);
if(s==NULL)
{
printf("\nno S struct");
return;
}
else
{
while(s!=NULL)
{
printf("\nmatricola:%d",s->matr);
s=s->next;
}
}
tmp=tmp->nextA;
}
}

这是我的第一篇文章,对于任何错误,我深表歉意。

最佳答案

在努力理解你想要做什么之后,我找到了你的问题,你必须改变你的 printS 函数。

void printS(struct A *T) {
struct A *tmp=T;
truct S *s = tmp->nextS;

while(tmp != NULL) {
printf("\nyear:%d", tmp->year);

if(s == NULL) {
printf("\nno S struct");
return ;
} else {
while(s != NULL) {
printf("\nmatricola:%d",s->matr);
s = s->next;
}
}
tmp = tmp->nextA;
}
}

像这样。

void printS(struct A *T) {
struct A *tmp=T;

while(tmp != NULL) {
struct S *s = tmp->nextS;
printf("\nyear:%d", tmp->year);

if(s == NULL) {
printf("\nno S struct");
return ;
} else {
while(s != NULL) {
printf("\nmatricola:%d",s->matr);
s = s->next;
}
}
tmp = tmp->nextA;
}
}

因为 struct S *s = tmp->nextS;必须更新为您所在的实际结构 A,因此它必须位于 while 循环内,如果您离开 struct S *s = tmp->nextS;在 while 循环之外,您将尝试打印从第一个结构 A 开始的结构 S 列表,而不是从每个结构 A 开始的整个结构 S 列表。

注意:正如我所说,尽量避免 fflush(stdin);因为如果参数不指向输出流,则行为未定义。

关于c++ - C丢失指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27493720/

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