整个代码在最后3个换行符之前是可以的,但是我想检查一下是否有一种方法可以正确地编写
完整代码:
float mat_add(float A[],float B[],float C[]);
{
x=0, y=0;
int row_num,colm_num,chk_row,chk_colm;
do
{
fflush(stdout);
printf("PLEASE! choose Array 'A' Rank (maximum 4x4)==>nn");
sleep(1);
printf(" a 'A' rows= ");
chk_row =scanf("%i", &row_num);
printf(" 'A' columns= ");
chk_colm =scanf("%i", &colm_num);
if( chk_row!=1||chk_colm!=1||row_num>4 || row_num<0|| colm_num>4 || colm_num<0) //restrictions for user input
{
printf("n a Failure!!----> Your Array Rank is not accepted !!.");
sleep(1);
printf("n n -----PLEASE!----- re-input your values ==>nn");
}
sleep(2);
}while( chk_row!=1||chk_colm!=1|| row_num>4 || row_num<0|| colm_num>4 || colm_num<0); //restrictions for user input
x=row_num ;
y=colm_num;
A[]=A[x][y];
printf("nnt a Success!!---> Array 'A' set to rank = %ix%i i.e. (A[%i][%i]). nn",row_num,colm_num,row_num,colm_num);
x,y全局声明!
我可以这样做A[]=A[2][3]吗?
签名:
float mat_add(float A[],float B[],float C[]);
传递指向三个数组的指针。因此,在调用该函数之前,数组的维数必须已经固定。
您没有将声明发布到这些数组中,因此我们无法知道(正如编写的那样,代码也无法知道)这些数组的维数。
所以当代码说:
A[]=A[x][y];
没有办法知道索引X
和Y
是否在这些数组的边界内。
表达式的这一部分:
A[]
以数组地址为目的(L值),将数组A[2][3]
中表项的内容赋值给??一个[0][0]? ?
这不是一个安全的赋值。在其他问题中,如果没有(数组中至少3行和至少4列)
此外,编译器不知道数组的每一行中有多少列,因此编译器不知道在内存中索引多远才能访问每一行(0
行之外)
结果是未定义行为
建议将签名修改为:
float mat_add( int rows, int columns, float A[ rows ][ columns ], etc ;
那么,在使用x
和y
之前,检查这些(用户输入的)变量是否为>0,对于x
<行和y
><列>列>行和CC_8>