简体   繁体   English

地址重合(指针,C编程)

[英]Addresses coincide (Pointers, C Programming)

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? 编译器或OS难道不应该这样做吗?

Also, why do i have to do ptr = (a+i*(n-1)+i+j); printf(" %d ",*ptr); 另外,为什么我必须做ptr = (a+i*(n-1)+i+j); printf(" %d ",*ptr); ptr = (a+i*(n-1)+i+j); printf(" %d ",*ptr); ?
Why won't printf(" %d ",*(a+i*(n-1)+i+j)); 为什么不printf(" %d ",*(a+i*(n-1)+i+j)); work? 工作?

First of all, a and b are arrays of pointers, and the pointers are never initialized. 首先, ab是指针数组,并且指针永远不会初始化。

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 (eg in %d ). 其次,您将指针视为ints (例如,在%d )。 Bear in mind that a pointer can be wider than an int . 请记住,指针可以比int宽。 For example, on my platform pointers are 64-bit and ints are 32-bit. 例如,在我的平台上,指针是64位, ints是32位。

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: 笔记:

  • when you get the rows and columns from the user, its better to use dynamic memory allocation functions like malloc() to allocate memory accordingly 当您从用户那里获得rowscolumns ,最好使用动态内存分配函数(例如malloc()来相应地分配内存
  • Any malloc() 'ed memory should be free() 'ed 任何malloc()编辑的内存都应该是free()编辑的
  • Your way of accessing an array cell like (a+i*(n-1)+i+j) is way too complex. 您访问(a+i*(n-1)+i+j)类的数组单元的方法过于复杂。 When dealing with pointers/arrays, its good to maintain simplicity. 处理指针/数组时,保持简单性很好。 Please try to stick to a[][] way of accessing an array location. 请尝试坚持a[][]方式访问数组位置。

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[][]). 我认为问题在于ab是指向int的指针的指针(int [] [] []),但是您的代码使用它们的方式就像它们是指向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. 因此,即使数组分配正确(不是这种情况),它们的地址也可能彼此靠近存储,从而导致这种意外行为。

As mention above, you probable mean to make a and b int[][] instead of int*[][]. 如上所述,您可能意味着将a和b设为int [] []而不是int * [] []。 In addition, you should not write a+i*(n-1)+i+j) , but &a[i][j] , or *(a+i)+j . 另外,您不应写a+i*(n-1)+i+j) ,而应写&a[i][j]*(a+i)+j (or another combine, like a[i]+j ). (或另一个组合,如a[i]+j )。 the compiler should automatically translate the address to the right member of the array. 编译器应自动将地址转换为数组的正确成员。

(sorry for my poor English) (抱歉我的英语不好)

ps Anyway, why did you write i*(n-1)+i and not simply (but, as I wrote above, wrong too) i*n ? ps无论如何,为什么您要写i*(n-1)+i而不是简单地写(但正如我上面所写的,也是错的) i*n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM