我有两个二维数组,我不知道为什么或如何,每个数组中的两个元素的地址重合。 这是源代码:
#include <stdio.h>
int main()
{
int i,j,m,n,o,p,*ptr;
printf("Enter dimension of 1st matrix: ");
scanf("%d * %d",&m,&n);
printf("Enter dimension of 2nd matrix: ");
scanf("%d * %d",&o,&p);
int *a[m][n];
int *b[o][p];
if (n!=o) return 0;
printf("\nEnter 1st matrix:\n");
for (i=0;i<m;i++)
for (j=0;j<n;j++)
{ printf("%d ",(a+i*(n-1)+i+j)); scanf("%d",(a+i*(n-1)+i+j)); }
printf("\nEnter 2nd matrix:\n");
for (i=0;i<o;i++)
for (j=0;j<p;j++)
{ printf("%d ",(b+i*(p-1)+i+j)); scanf("%d",(b+i*(p-1)+i+j)); }
/*Printing the matrices*/
puts("");puts("");
for (i=0;i<m;i++)
{for (j=0;j<n;j++)
{ ptr = (a+i*(n-1)+i+j);
printf(" %d ",*ptr); } puts("");}puts("");
for (i=0;i<o;i++)
{for (j=0;j<p;j++)
{ ptr = (b+i*(p-1)+i+j);
printf(" %d ",*ptr); } puts("");}
}
这是一个打印屏幕;
由于这个原因,我在一个简单的程序中得到错误来计算两个矩阵的乘积。问题是,这是通常的吗?编译器或操作系统不应该处理这个吗?
另外,为什么我必须ptr = (a+i*(n-1)+i+j); printf(" %d ",*ptr);
?
为什么printf(" %d ",*(a+i*(n-1)+i+j));
不起作用?
答案 0 :(得分:6)
首先,a
和b
是指针数组,指针永远不会被初始化。
int *a[m][n];
int *b[o][p];
我的猜测是它的意思是:
int a[m][n];
int b[o][p];
(其余代码需要相应更改。)
其次,您将指针视为ints
(例如在%d
中)。请记住,指针可以比int
宽。例如,在我的平台上,指针是64位,ints
是32位。
答案 1 :(得分:1)
我看到了多个问题,所以重新编写了如下程序:
#include <stdio.h>
#include <stdlib.h>
void display(int **matrix, int r, int c)
{
int i, j;
for (i=0 ; i<r ; i++) {
for (j=0 ; j<c; j++) {
printf("%3d ", matrix[i][j]);
}
printf("\n");
}
return;
}
int main(void)
{
int r1, c1, r2, c2;
int **matrix1, **matrix2;
int i, j;
printf("Enter r1: ");
scanf("%d", &r1);
printf("Enter c1: ");
scanf("%d", &c1);
if ((matrix1 = (int **) malloc (sizeof(int *) * r1)) == NULL) {
printf("unable to allocate memeory \n");
return -1;
};
for (i=0 ; i<r1 ; i++) {
if ((matrix1[i] = malloc (sizeof(int) * c1)) == NULL) {
printf("unable to allocate memory \n");
return -1;
}
}
printf("Enter contents of matrix 1\n");
for (i=0 ; i<r1 ; i++) {
for (j=0 ; j<c1; j++) {
printf("matrix1[%d][%d] :", i, j);
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter r2: ");
scanf("%d", &r2);
printf("Enter c2: ");
scanf("%d", &c2);
if ((matrix2 = (int **) malloc (sizeof(int *) * r2)) == NULL) {
printf("unable to allocate memeory \n");
return -1;
};
for (i=0 ; i<r2 ; i++) {
if ((matrix2[i] = malloc (sizeof(int) * c2)) == NULL) {
printf("unable to allocate memory \n");
return -1;
}
}
printf("Enter contents of matrix 2\n");
for (i=0 ; i<r2 ; i++) {
for (j=0 ; j<c2; j++) {
printf("matrix1[%d][%d] :", i, j);
scanf("%d", &matrix2[i][j]);
}
}
printf("Contents of matrix 1 is as follows \n");
display(matrix1, r1, c1);
printf("\n\n");
printf("Contents of matrix 2 is as follows \n");
display(matrix2, r2, c2);
/* now, free the contents of the matrix 1 and 2 */
for (i=0 ; i<r1 ; i++)
free(matrix1[i]);
free(matrix1);
for (i=0 ; i<r2 ; i++)
free(matrix2[i]);
free(matrix2);
return 0;
}
输出
$ gcc 2d.c
$ ./a.out
Enter r1: 2
Enter c1: 2
Enter contents of matrix 1
matrix1[0][0] :1
matrix1[0][1] :2
matrix1[1][0] :3
matrix1[1][1] :4
Enter r2: 5
Enter c2: 6
Enter contents of matrix 2
matrix1[0][0] :1
matrix1[0][1] :2
matrix1[0][2] :3
matrix1[0][3] :4
matrix1[0][4] :5
matrix1[0][5] :6
matrix1[1][0] :7
matrix1[1][1] :8
matrix1[1][2] :9
matrix1[1][3] :0
matrix1[1][4] :1
matrix1[1][5] :2
matrix1[2][0] :3
matrix1[2][1] :4
matrix1[2][2] :5
matrix1[2][3] :6
matrix1[2][4] :7
matrix1[2][5] :8
matrix1[3][0] :9
matrix1[3][1] :0
matrix1[3][2] :1
matrix1[3][3] :2
matrix1[3][4] :3
matrix1[3][5] :4
matrix1[4][0] :5
matrix1[4][1] :6
matrix1[4][2] :7
matrix1[4][3] :8
matrix1[4][4] :9
matrix1[4][5] :0
Contents of matrix 1 is as follows
1 2
3 4
Contents of matrix 2 is as follows
1 2 3 4 5 6
7 8 9 0 1 2
3 4 5 6 7 8
9 0 1 2 3 4
5 6 7 8 9 0
$
注意:
rows
和columns
时,最好使用动态内存分配函数(如malloc()
)来相应地分配内存malloc()
'内存应为free()
'ed (a+i*(n-1)+i+j)
这样的数组单元格的方式过于复杂。在处理指针/数组时,它很好地保持了简单性。请尝试坚持a[][]
方式访问阵列位置。 答案 2 :(得分:0)
我认为问题是a
和b
是指向指向int(int [] [] [])的指针的指针,但是你的代码使用它们就好像它们是指向指针的指针一样int(int [] [])。因此,即使数组分配正确(事实并非如此),它们的地址也可能彼此靠近存储,从而导致这种意外行为。
答案 3 :(得分:0)
如上所述,你可能意味着使a和b int [] []而不是int * [] []。
此外,您不应该写a+i*(n-1)+i+j)
,而是&a[i][j]
或*(a+i)+j
。 (或其他组合,如a[i]+j
)。
编译器应自动将地址转换为数组的正确成员。
(抱歉我的英语不好)
P.S。无论如何,为什么你写i*(n-1)+i
而不是简单(但是,正如我上面写的那样,也是错误的)i*n
?