无法访问C中二维数组的位置
Can't access a position of Bidimensional array in C
大家好,我一直在尝试解决 C 中的乘法问题。我现在无法从 Integer.By 的数组中获取一个元素,我只能访问第一行数组 A[][] 的值,但其他值不正确。这是代码片段,问题出在 matrixProduct 方法中:
int fa,ca,fb,cb=0;
int main(){
int A[20][20], B[20][20]. C[20][20];
printf("Rows for A: ");
scanf("%d",&fa);
printf("\nColumns for A: ");
scanf("%d",&ca);
printf("\nRows for B: ");
scanf("%d", &fb);
printf("\nColumns for B: ");
scanf("%d",&cb);
if (ca!=fb){
printf("\nOperation not available");
return 0;
}else{//Filling matrix A & B
int i,j;
for (i=0; i<fa; i++){
for(j=0;j<ca;j++){
printf("Element at position A[%d][%d]: ",i,j);
scanf("%d",&A[i][j]);
}
}
for (i=0; i<fb; i++){
for(j=0;j<cb;j++){
printf("Element at position B[%d][%d]: ",i,j);
scanf("%d",&B[i][j]);
}
}
}
int k=productoMatriz(A,B);
return 0;
}
//Parameters two matrixes A,B
int matrixProduct(int A[fa][ca],int B[fb][cb]){
int i,j,k=0;
int C[20][20];
for (i=0; i<fa; i++){
for(j=0;j<cb;j++){
C[i][j]=0;
}
}
for(i=0;i<fa;i++){
for(j=0;j<cb;j++){
printf(" A [%d] [%d] = %d\n",i,j,A[i][j]);//Problem is in here works
//ok for the first row but then it doesnt!
for(k=0;k<ca;k++){
//C[i][j]=A[i][k]*B[k][j];
}
}
}
printf("\n\nC Matrix:\n");
for(i=0; i<fa; i++)
for(j=0; j<cb; j++)
{
printf("%d ",C[i][j]);
if(j==cb-1)
printf("\n\n");
}
return 0;
}
编辑
现在我已经将 20 的值更改为宏并更改了 matrixProduct 中的参数,但现在我正在尝试 return main 方法中的 C 值,类似这样,然后在其他方法中打印它void printMatrix(int C[MAXSIZE][MAXSIZE]):
C[MAXSIZE][MAXSIZE]=matrixProduct(A,B);
printMatrix(C);
我又得到了错误的值...
问题是您正在为 variable-size fa
通过 ca
和 fb
通过 cb
传递 fixed-size 20 x 20 数组] 数组。这是不允许的:您接受数组的方式应该与声明数组的方式相同:
int matrixProduct(int A[20][20], int B[20][20])
否则,编译器将从内存中的错误位置访问数据,从而导致未定义的行为。
或者,您可以将 main
内的 A
和 B
的声明移动到 fa
、ca
、[=13= 之后的某个点], 和 cb
被读取,并以与在 matrixProduct
函数的 header 中声明它们相同的方式声明它们。
此外,matrixProduct
函数似乎缺少原型。你应该收到一个警告。
问题出在这里:
int matrixProduct(int A[fa][ca],int B[fb][cb]){
您的函数原型需要 int [fa][ca]
和 int [fb][cb]
,但您传入的是 int [20][20]
和 int [20][20]
。这些是不同大小的对象,因此当您尝试访问它从错误地址提取的数组元素时。
一个二维数组的布局是每一行都在下一行之后。所以如果你有 int A[2][2]
它看起来像这样:
| 0 | 1 | 2 | 3 |
[0,0] [0,1] [1,0] [1,1]
同样,如果您有 int A[3][3]
,它看起来像这样:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
[0,0] [0,1] [0,2] [1,0] [1,1] [1,2] [2,0] [2,1] [2,2]
在第一种情况下,[1,0]
在第三个字节,而在第二种情况下,它在第四个字节。
编辑:
解决您的编辑问题,这行不通:
C[MAXSIZE][MAXSIZE]=matrixProduct(A,B);
因为你不能 return 函数中的数组。您需要做的是将二维数组 C
作为参数传递。因为数组作为指向第一个元素的指针传递给函数,所以对数组所做的更改会反映在函数外部。
其他答案可能会指出您的错误。但据我所知,以下几点
- 打印不需要 return 函数。
- 因此,应使用 void 函数。
- 您应该使用宏来指定数组的大小。因为当你想增加或减少数组的大小时,它会很方便。
- 原型和参数中可能没有写第一维的大小。但第二必须。
- 只要有可能和有用的情况,尽量发挥作用。
- 您想
return
和 0
,没关系,但用于错误处理
你应该选择另一个数字,比如 -1
.
- 尽可能避免使用全局变量! Why?
- 我还建议在定义函数之前先声明函数原型,因为如果您编写了错误类型的变量或未命中,编译器会警告您。
- 您可以使用代码,但检查您的错误。
我已尝试改进您的代码
#include <stdio.h>
#define SIZE 20
void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb);
void multiplication(int a[][SIZE],int b[][SIZE],int mult[][SIZE],int fa,int ca,int fb,int cb);
void display(int mult[][SIZE], int fa, int cb);
int main()
{
int a[SIZE][SIZE], b[SIZE][SIZE], mult[SIZE][SIZE], fa, ca, fb, cb;
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &fa, &ca);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&fb, &cb);
/* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */
while (ca!=fb)
{
printf("Error! column of first matrix not equal to row of second.\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &fa, &ca);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&fb, &cb);
}
take_data(a,b,fa,ca,fb,cb); /* Function to take matices data */
multiplication(a,b,mult,fa,ca,fb,cb); /* Function to multiply two matrices. */
display(mult,fa,cb); /* Function to display resultant matrix after multiplication. */
return 0;
}
void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb)
{
int i,j;
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<fa; ++i)
for(j=0; j<ca; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<fb; ++i)
for(j=0; j<cb; ++j)
{
printf("Enter elements b%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
void multiplication(int a[][SIZE],int b[][SIZE],int mult[][SIZE],int fa,int ca,int fb,int cb)
{
int i,j,k;
/* Initializing elements of matrix mult to 0.*/
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
{
mult[i][j]=0;
}
/* Multiplying matrix a and b and storing in array mult. */
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
for(k=0; k<ca; ++k)
{
mult[i][j]+=a[i][k]*b[k][j];
}
}
void display(int mult[][SIZE], int fa, int cb)
{
int i, j;
printf("\nOutput Matrix:\n");
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
{
printf("%d ",mult[i][j]);
if(j==cb-1)
printf("\n\n");
}
}
我们需要使用malloc()
到return数组并且不要忘记free
it.Withreturn数组
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 20
void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb);
int** multiplication(int a[][SIZE],int b[][SIZE],int fa,int ca,int fb,int cb);
void display(int fa, int cb, int** mult);
void free_mult(int **mult, int Rows);
int main()
{
int a[SIZE][SIZE], b[SIZE][SIZE], fa, ca, fb, cb;
int** mult;
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &fa, &ca);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&fb, &cb);
/* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */
while (ca!=fb)
{
printf("Error! column of first matrix not equal to row of second.\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &fa, &ca);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&fb, &cb);
}
take_data(a,b,fa,ca,fb,cb); /* Function to take matices data */
mult = multiplication(a,b,fa,ca,fb,cb); /* Function to multiply two matrices. */
display(fa,cb,mult); /* Function to display resultant matrix after multiplication. */
free_mult(mult, fa);
return 0;
}
void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb)
{
int i,j;
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<fa; ++i)
for(j=0; j<ca; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<fb; ++i)
for(j=0; j<cb; ++j)
{
printf("Enter elements b%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
int** multiplication(int a[][SIZE],int b[][SIZE],int fa,int ca,int fb,int cb)
{
int i,j,k;
int **mult = (int **)malloc(fa * sizeof(int *));
int row;
// for each row allocate Cols ints
for (row = 0; row < fa; row++) {
mult[row] = (int *)malloc(fa * sizeof(int));
}
/* Initializing elements of matrix mult to 0.*/
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
{
mult[i][j]=0;
}
/* Multiplying matrix a and b and storing in array mult. */
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
for(k=0; k<ca; ++k)
{
mult[i][j]+=a[i][k]*b[k][j];
}
return mult;
}
void display(int fa, int cb, int** mult)
{
int i, j;
printf("\nOutput Matrix:\n");
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
{
printf("%d ",mult[i][j]);
if(j==cb-1)
printf("\n\n");
}
}
void free_mult(int **mult, int Rows)
{
int row;
// first free each row
for (row = 0; row < Rows; row++) {
free(mult[row]);
}
// Eventually free the memory of the pointers to the rows
free(mult);
}
大家好,我一直在尝试解决 C 中的乘法问题。我现在无法从 Integer.By 的数组中获取一个元素,我只能访问第一行数组 A[][] 的值,但其他值不正确。这是代码片段,问题出在 matrixProduct 方法中:
int fa,ca,fb,cb=0;
int main(){
int A[20][20], B[20][20]. C[20][20];
printf("Rows for A: ");
scanf("%d",&fa);
printf("\nColumns for A: ");
scanf("%d",&ca);
printf("\nRows for B: ");
scanf("%d", &fb);
printf("\nColumns for B: ");
scanf("%d",&cb);
if (ca!=fb){
printf("\nOperation not available");
return 0;
}else{//Filling matrix A & B
int i,j;
for (i=0; i<fa; i++){
for(j=0;j<ca;j++){
printf("Element at position A[%d][%d]: ",i,j);
scanf("%d",&A[i][j]);
}
}
for (i=0; i<fb; i++){
for(j=0;j<cb;j++){
printf("Element at position B[%d][%d]: ",i,j);
scanf("%d",&B[i][j]);
}
}
}
int k=productoMatriz(A,B);
return 0;
}
//Parameters two matrixes A,B
int matrixProduct(int A[fa][ca],int B[fb][cb]){
int i,j,k=0;
int C[20][20];
for (i=0; i<fa; i++){
for(j=0;j<cb;j++){
C[i][j]=0;
}
}
for(i=0;i<fa;i++){
for(j=0;j<cb;j++){
printf(" A [%d] [%d] = %d\n",i,j,A[i][j]);//Problem is in here works
//ok for the first row but then it doesnt!
for(k=0;k<ca;k++){
//C[i][j]=A[i][k]*B[k][j];
}
}
}
printf("\n\nC Matrix:\n");
for(i=0; i<fa; i++)
for(j=0; j<cb; j++)
{
printf("%d ",C[i][j]);
if(j==cb-1)
printf("\n\n");
}
return 0;
}
编辑
现在我已经将 20 的值更改为宏并更改了 matrixProduct 中的参数,但现在我正在尝试 return main 方法中的 C 值,类似这样,然后在其他方法中打印它void printMatrix(int C[MAXSIZE][MAXSIZE]):
C[MAXSIZE][MAXSIZE]=matrixProduct(A,B);
printMatrix(C);
我又得到了错误的值...
问题是您正在为 variable-size fa
通过 ca
和 fb
通过 cb
传递 fixed-size 20 x 20 数组] 数组。这是不允许的:您接受数组的方式应该与声明数组的方式相同:
int matrixProduct(int A[20][20], int B[20][20])
否则,编译器将从内存中的错误位置访问数据,从而导致未定义的行为。
或者,您可以将 main
内的 A
和 B
的声明移动到 fa
、ca
、[=13= 之后的某个点], 和 cb
被读取,并以与在 matrixProduct
函数的 header 中声明它们相同的方式声明它们。
此外,matrixProduct
函数似乎缺少原型。你应该收到一个警告。
问题出在这里:
int matrixProduct(int A[fa][ca],int B[fb][cb]){
您的函数原型需要 int [fa][ca]
和 int [fb][cb]
,但您传入的是 int [20][20]
和 int [20][20]
。这些是不同大小的对象,因此当您尝试访问它从错误地址提取的数组元素时。
一个二维数组的布局是每一行都在下一行之后。所以如果你有 int A[2][2]
它看起来像这样:
| 0 | 1 | 2 | 3 |
[0,0] [0,1] [1,0] [1,1]
同样,如果您有 int A[3][3]
,它看起来像这样:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
[0,0] [0,1] [0,2] [1,0] [1,1] [1,2] [2,0] [2,1] [2,2]
在第一种情况下,[1,0]
在第三个字节,而在第二种情况下,它在第四个字节。
编辑:
解决您的编辑问题,这行不通:
C[MAXSIZE][MAXSIZE]=matrixProduct(A,B);
因为你不能 return 函数中的数组。您需要做的是将二维数组 C
作为参数传递。因为数组作为指向第一个元素的指针传递给函数,所以对数组所做的更改会反映在函数外部。
其他答案可能会指出您的错误。但据我所知,以下几点
- 打印不需要 return 函数。
- 因此,应使用 void 函数。
- 您应该使用宏来指定数组的大小。因为当你想增加或减少数组的大小时,它会很方便。
- 原型和参数中可能没有写第一维的大小。但第二必须。
- 只要有可能和有用的情况,尽量发挥作用。
- 您想
return
和0
,没关系,但用于错误处理 你应该选择另一个数字,比如-1
. - 尽可能避免使用全局变量! Why?
- 我还建议在定义函数之前先声明函数原型,因为如果您编写了错误类型的变量或未命中,编译器会警告您。
- 您可以使用代码,但检查您的错误。
我已尝试改进您的代码
#include <stdio.h>
#define SIZE 20
void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb);
void multiplication(int a[][SIZE],int b[][SIZE],int mult[][SIZE],int fa,int ca,int fb,int cb);
void display(int mult[][SIZE], int fa, int cb);
int main()
{
int a[SIZE][SIZE], b[SIZE][SIZE], mult[SIZE][SIZE], fa, ca, fb, cb;
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &fa, &ca);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&fb, &cb);
/* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */
while (ca!=fb)
{
printf("Error! column of first matrix not equal to row of second.\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &fa, &ca);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&fb, &cb);
}
take_data(a,b,fa,ca,fb,cb); /* Function to take matices data */
multiplication(a,b,mult,fa,ca,fb,cb); /* Function to multiply two matrices. */
display(mult,fa,cb); /* Function to display resultant matrix after multiplication. */
return 0;
}
void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb)
{
int i,j;
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<fa; ++i)
for(j=0; j<ca; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<fb; ++i)
for(j=0; j<cb; ++j)
{
printf("Enter elements b%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
void multiplication(int a[][SIZE],int b[][SIZE],int mult[][SIZE],int fa,int ca,int fb,int cb)
{
int i,j,k;
/* Initializing elements of matrix mult to 0.*/
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
{
mult[i][j]=0;
}
/* Multiplying matrix a and b and storing in array mult. */
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
for(k=0; k<ca; ++k)
{
mult[i][j]+=a[i][k]*b[k][j];
}
}
void display(int mult[][SIZE], int fa, int cb)
{
int i, j;
printf("\nOutput Matrix:\n");
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
{
printf("%d ",mult[i][j]);
if(j==cb-1)
printf("\n\n");
}
}
我们需要使用malloc()
到return数组并且不要忘记free
it.Withreturn数组
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 20
void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb);
int** multiplication(int a[][SIZE],int b[][SIZE],int fa,int ca,int fb,int cb);
void display(int fa, int cb, int** mult);
void free_mult(int **mult, int Rows);
int main()
{
int a[SIZE][SIZE], b[SIZE][SIZE], fa, ca, fb, cb;
int** mult;
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &fa, &ca);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&fb, &cb);
/* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */
while (ca!=fb)
{
printf("Error! column of first matrix not equal to row of second.\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &fa, &ca);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&fb, &cb);
}
take_data(a,b,fa,ca,fb,cb); /* Function to take matices data */
mult = multiplication(a,b,fa,ca,fb,cb); /* Function to multiply two matrices. */
display(fa,cb,mult); /* Function to display resultant matrix after multiplication. */
free_mult(mult, fa);
return 0;
}
void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb)
{
int i,j;
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<fa; ++i)
for(j=0; j<ca; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<fb; ++i)
for(j=0; j<cb; ++j)
{
printf("Enter elements b%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
int** multiplication(int a[][SIZE],int b[][SIZE],int fa,int ca,int fb,int cb)
{
int i,j,k;
int **mult = (int **)malloc(fa * sizeof(int *));
int row;
// for each row allocate Cols ints
for (row = 0; row < fa; row++) {
mult[row] = (int *)malloc(fa * sizeof(int));
}
/* Initializing elements of matrix mult to 0.*/
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
{
mult[i][j]=0;
}
/* Multiplying matrix a and b and storing in array mult. */
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
for(k=0; k<ca; ++k)
{
mult[i][j]+=a[i][k]*b[k][j];
}
return mult;
}
void display(int fa, int cb, int** mult)
{
int i, j;
printf("\nOutput Matrix:\n");
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
{
printf("%d ",mult[i][j]);
if(j==cb-1)
printf("\n\n");
}
}
void free_mult(int **mult, int Rows)
{
int row;
// first free each row
for (row = 0; row < Rows; row++) {
free(mult[row]);
}
// Eventually free the memory of the pointers to the rows
free(mult);
}