请教下C语言高手,程序老是报错 `a' was not declared in this scope
10.编写…个程序,声明一个3x5的数组并初始化,具体数值可以随意。程序打印出数值,然后数值翻1番,接着再次打印出新值。编写一个函数来显示数组的内容,再编写另一个函数执行...
10.编写…个程序,声明一个3x5的数组并初始化,具体数值可以随意。程序打印出数值,然后数值翻1番,接着再次打印出新值。编写一个函数来显示数组的内容,再编写另一个函数执行翻倍功能。数组名和数组行数作为参数由程序传递给函数。
#include<stdio.h>
#include<stdlib.h>
#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 展开
#include<stdio.h>
#include<stdlib.h>
#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和b是个变量,所以是不允许的
改了下:
#include<stdio.h>
#include<stdlib.h>
#define SIZE1 3
#define SIZE2 5
void mul(int a, int b, double arr[][SIZE2]);
void echo(int a,int b,double arr[][SIZE2]);
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[][SIZE2])
{
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[][SIZE2])
{
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]);
};
}
改了下:
#include<stdio.h>
#include<stdlib.h>
#define SIZE1 3
#define SIZE2 5
void mul(int a, int b, double arr[][SIZE2]);
void echo(int a,int b,double arr[][SIZE2]);
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[][SIZE2])
{
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[][SIZE2])
{
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]);
};
}
追问
我写的是变长数组吧
展开全部
经我用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,这些可以在预处理后的代码里看到。
void mul(int a, int b, double arr[][SIZE2])
void echo(int a,int b,double arr[][SIZE2])
你写的不是变长数组,因为你用的SIZE1 和SIZE2是宏,宏在预处理的时候都会被换成原本的。也就是说:预处理后,SIZE1变成了3,SIZE2变成了5,这些可以在预处理后的代码里看到。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
void mul(int a, int b, double arr[a][b]);的定义里,double arr[a][b],是错的,这里不能用a ,b
追问
那应用什么?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
数组传参,传的是首地址。只要传2个参数,1个是数组名(char *)和另一个是数组长度(int)。当然,多维数组传多个数组长度(int)。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
void mul(int a, int b, double arr[a][b]);的定义里,double arr[a][b],是错的,这里不能用a ,b
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询