Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未在此范围内声明(C语言中的错误)_C_Function_Struct - Fatal编程技术网

未在此范围内声明(C语言中的错误)

未在此范围内声明(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(

我有以下几段代码: 标题:

功能原型(仅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("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
的范围。在使用onother
HOTEL newh
inner
main()
并将其作为参数发送给其他函数之前,您需要声明onother
HOTEL 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;