【发布时间】:2015-04-15 15:51:28
【问题描述】:
我有这些代码: 标题:
#ifndef hotel
#define hotel
typedef struct hotel
{
char name_of_H[50];
char adress[50];
int nr_rooms;
int floors;
float stars;
}HOTEL;
void inputh(HOTEL *h, int n);
void outputh(HOTEL *h, int n);
int searchh( HOTEL *h, int n, char *key);
void swaph( HOTEL *h, int k1, int k2);
void sorth( HOTEL *h, int n);
void modifyh(HOTEL *h, int k);
int writeh(HOTEL *h, int n, char * fname);
HOTEL* appendh(HOTEL *h, int *n,HOTEL newh);
HOTEL* inserth(HOTEL *h, int *n, int k,HOTEL newh);
HOTEL* delh(HOTEL *h , int *n, int k);
#endif
函数原型(仅 2 个):
HOTEL* appendh(HOTEL *h, int *n, HOTEL newh)
{ int i;
HOTEL *p;
p=(HOTEL *)realloc(h,(*n+1)*sizeof(*p));
if (p==NULL) {return p;}
p[*n]= newh;
*n=*n+1;
return p;
}
HOTEL* inserth(HOTEL *h, int *n, int k,HOTEL newh)
{ int i;
HOTEL *p;
p=(HOTEL*)realloc(h,(*n+1)*sizeof(*p));
if (p==NULL) {return p;}
for(i=*n; i>k; i--)
{ p[i]= p[i-1]; }
p[k]= newh;
*n=(*n)+1;
return p;
}
主要是:
printf("Enter information about hotel:");
printf("Name of hotel: "); fflush(stdin);
gets(newh.name_of_H);//here is the error
printf("Adress of hotel: "); fflush(stdin);
gets(newh.adress);
printf("Number f rooms: "); scanf("%d", &newh.nr_rooms);
printf("Floors: "); scanf("%d", &newh.floors);
printf("Stars: "); scanf("%f", &newh.stars);
printf("Enter position for insertion: ");
scanf("%d", &k);
p=inserth(h, n, k, newh); if (p==NULL)
{puts("memory was not reallocated");}
else {h=p;}
getch( ); break;
('newh is not decl. in this scope')Newh 是一个结构变量,具有字段名称、地址、...为什么我在尝试输入信息时出错?谢谢!
【问题讨论】:
-
C语言没有错误。
-
你认为
newh是在哪里声明的? Post an MCVE -
哦,顺便说一句,在输入流中执行
fflush(如stdin)在技术上是未定义的行为。一些库允许它作为扩展,但不要指望它用于可移植程序。 -
对于代表为 1 的人来说,在问题标题中添加“语言错误”总是一个好主意。
-
这是完整的主要内容吗?我不这么认为。如果是,您从未定义过 newh。