Reputation: 1002
I have two two-dimensional arrays, and i don't know why, or how, the addresses of two elements one from each array, coincide.. Here's the source code:
#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("");}
}
And here's a print screen;
Due to this, i have been getting errors in a simple program to calculate the product of two matrices. The question is, is this usual? Shouldn't the compiler or the OS have taken care of this?
Also, why do i have to do ptr = (a+i*(n-1)+i+j); printf(" %d ",*ptr);
?
Why won't printf(" %d ",*(a+i*(n-1)+i+j));
work?
Upvotes: 2
Views: 199
Reputation: 5456
As mention above, you probable mean to make a and b int[][] instead of int*[][].
In addition, you should not write a+i*(n-1)+i+j)
, but &a[i][j]
, or *(a+i)+j
. (or another combine, like a[i]+j
).
the compiler should automatically translate the address to the right member of the array.
(sorry for my poor English)
p.s. Anyway, why did you write i*(n-1)+i
and not simply (but, as I wrote above, wrong too) i*n
?
Upvotes: 0
Reputation: 16597
I saw multiple problems so re-wrote the program as follows:
#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;
}
Output
$ 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
$
Notes:
rows
and columns
from the user, its better to use dynamic memory allocation functions like malloc()
to allocate memory accordinglymalloc()
'ed memory should be free()
'ed(a+i*(n-1)+i+j)
is way too complex. When dealing with pointers/arrays, its good to maintain simplicity. Please try to stick to a[][]
way of accessing an array location. Upvotes: 1
Reputation: 3972
I think the problem is that a
and b
are pointers to pointers to pointers to int (int[][][]) but your code uses them as if they were pointers to pointers to int (int[][]). Because of that, even if the arrays were allocated correctly (which isn't the case) their adresses could be stored close to each other causing this unexpected behavior.
Upvotes: 0
Reputation: 500257
First of all, a
and b
are arrays of pointers, and the pointers are never initialized.
int *a[m][n];
int *b[o][p];
My guess is that it was meant to read:
int a[m][n];
int b[o][p];
(The rest of the code would need to be changed accordingly.)
Secondly, you're treating pointers as ints
(e.g. in %d
). Bear in mind that a pointer can be wider than an int
. For example, on my platform pointers are 64-bit and ints
are 32-bit.
Upvotes: 6