作业帮 > 综合 > 作业

程序老是报错 `a' was not declared in this scope

来源:学生作业帮 编辑:灵鹊做题网作业帮 分类:综合作业 时间:2024/05/03 12:45:39
程序老是报错 `a' was not declared in this scope
10.编写…个程序,声明一个3x5的数组并初始化,具体数值可以随意.程序打印出数值,然后数值翻1番,接着再次打印出新值.编写一个函数来显示数组的内容,再编写另一个函数执行翻倍功能.数组名和数组行数作为参数由程序传递给函数.
#include
#include
#define SIZE1 3
#define SIZE2 5
void mul(int a,int b,double arr[a][b]);
void echo(int a,int b,double arr[a][b]);
int main(void)
{
double one[SIZE1][SIZE2] =
{
{1.1,2.2,3.3,4.4,5.5},
{6.6,7.7,8.8,9.9,10.10},
{11.11,12.12,13.13,14.14,15.15}
};
echo(SIZE1,SIZE2,one);
mul(SIZE1,SIZE2,one);
echo(SIZE1,SIZE2,one);
system("pause");
return 0;
}
void mul(int a,int b,double arr[a][b])
{
int i,n;
for(i = 0;i < a;i++)
{
for(n = 0;n < b;n++)
arr[i][n] *= 2;
};
}
void echo(int a,int b,double arr[a][b])
{
int i,n;
for(i = 0;i < a;i++)
{
printf("\n[%d]\n",i);
for(n = 0;n < b;n++)
printf("%7lg",arr[i][n]);
};
}
编译器用的是dev c++,
5:error:`a' was not declared in this scope
5:error:`b' was not declared in this scope
6:error:`a' was not declared in this scope
6:error:`b' was not declared in this scope
In function `int main()':
6:error:too many arguments to function `void echo(int,int)'
16:error:at this point in file
5:error:too many arguments to function `void mul(int,int)'
17:error:at this point in file
6:error:too many arguments to function `void echo(int,int)'
18:error:at this point in file
At global scope:
22:error:`a' was not declared in this scope
22:error:`b' was not declared in this scope
In function `void mul(int,int)':
28:error:`arr' undeclared (first use this function)
28:error:(Each undeclared identifier is reported only once for each function it appears in.)
At global scope:
31:error:`a' was not declared in this scope
31:error:`b' was not declared in this scope
In function `void echo(int,int)':
38:error:`arr' undeclared
程序老是报错 `a' was not declared in this scope
经我用devcpp测试发现应该定义成
void mul(int a,int b,double arr[][SIZE2])
void echo(int a,int b,double arr[][SIZE2])
你写的不是变长数组,因为你用的SIZE1 和SIZE2是宏,宏在预处理的时候都会被换成原本的.也就是说:预处理后,SIZE1变成了3,SIZE2变成了5,这些可以在预处理后的代码里看到.