【问题标题】:Not declared in this scope(error in C language)未在此范围内声明(C 语言错误)
【发布时间】: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。

标签: c function struct


【解决方案1】:

正如我在您的代码中看到的,HOTEL newh 被提及为函数 appendhinserth 的参数,因此这两个变量对于这两个函数的主体(appendhinserth,而不是在main)。这意味着,函数main 无法到达HOTEL newh 的范围。在使用之前,您需要在 main() 中声明 onother HOTEL newh,并将其作为参数发送给其他函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多