为什么代码运行,报错?哪里写错了么?
发布网友
发布时间:2023-09-14 12:02
我来回答
共4个回答
热心网友
时间:2024-08-19 03:36
错的太多了吧,而且逻辑上也是...可能知道这个程序想干嘛,但不知道它实际在干嘛,比如
printf("\nHow old is %s? ",current->name);
canf("%d",¤t->age);/* read the horse's age */追问其实我就是想知道,为什么这代码编译通过,运行的时候出错而已,指针那块写错了么?
追答printf("\nEnter the name: ");
scanf("%s",current->name);
current->previous = last;//你没有指定前者吧!!!
current->next=NULL;
last=current;
热心网友
时间:2024-08-19 03:37
struct horse *first=NULL;/* Pointer to first horse */
struct horse *current=NULL;/* Pointer to current horse */
struct horse *last=NULL;/* Pointer to previous horse */
有的结构体指针未分配内存空间追问那要怎么改呢?
热心网友
时间:2024-08-19 03:37
出错信息?
热心网友
时间:2024-08-19 03:38
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main(void)
{
struct horse /* Structure declaration */
{
int age;
int height;
char name[20];
char father[20];
char mother[20];
struct horse *next; /* Pointer to next structure */
struct horse *previous; /* Pointer to previous structure */
};
struct horse *first=NULL; /* Pointer to first horse */
struct horse *current=NULL; /* Pointer to current horse */
struct horse *last=NULL; /* Pointer to previous horse */
char test='\0'; /* test value for ending input */
for(;;)
{
printf("\nDo you want to enter details of a%s horse (Y or N)? ",
first==NULL?"":"nother");
scanf(" %c",&test);
if(tolower(test)=='n')
break;
current=(struct horse*)malloc(sizeof(struct horse));
if(first==NULL)
{
first=current; /* Set pointer to first horse */
current->previous=NULL;
}
else
{
last->next=current; /* Set next address for previous horse */
}
printf("\nEnter the name of the horse: ");
scanf("%s",current->name); /* read the horse's name */
printf("\nHow old is %s? ",current->name);
scanf("%d",¤t->age); /* read the horse's age */
printf("\nHow high is %s (in hands)? ",current->name);
scanf("%d",¤t->height); /* read the horse's height */
printf("\nWho is %s's father? ",current->name);
scanf("%s",current->father); /* Get the father's name */
printf("\nWho is %s's mother? ",current->name);
scanf("%s",current->mother); /* Get the mother's name */
current->next=NULL; /* In case it's the last horse.. */
last=current; /* Save address of last horse */
}
/* Now tell them what we know. */
while(current!=NULL) /* output horse data in reverse order */
{
printf("\n\n%s is %d years old,%d hands high, and has %s and %s as parents."
,current->name,current->age,current->height,current->father,current->mother);
last=current; /* Save pointer to enable memory to be freed */
current=current->previous; /* current points to previous in list */
free(last); /* free memory for the horse we output */
}
return 0;
}追问还没找到重点。。