未在此范围内声明(C语言中的错误)
我有以下几段代码: 标题: 功能原型(仅2个): 总的来说:未在此范围内声明(C语言中的错误),c,function,struct,C,Function,Struct,我有以下几段代码: 标题: 功能原型(仅2个): 总的来说: 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(
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不是decl.在此范围内')newh是一个结构变量,具有字段名称、地址,…为什么我在尝试输入信息时出错?谢谢 正如我在您的代码中看到的,
HOTEL newh
被提到为函数的参数append
和inserth
,所以这两个变量是这两个函数主体的局部变量(append
和inserth
,而不是在main
)。也就是说,函数main
无法到达HOTEL newh
的范围。在使用onotherHOTEL newh
innermain()
并将其作为参数发送给其他函数之前,您需要声明onotherHOTEL newh
。C语言中没有错误。您认为newh
是在哪里声明的?顺便说一下,在输入流中执行fflush
(如stdin
)在技术上是未定义的行为。有些库允许将其作为扩展,但不能将其用于可移植程序。对于rep为1的人来说,在问题标题中添加“语言错误”总是一个好主意。这是完整的主要问题吗?我不这么认为。如果是,你从来没有定义过newh。
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;