在编译C程序以使用malloc()和free()从m×n矩阵中查找重复值时出现错误

问题描述 投票:0回答:1

所以我正在解决此问题,但出现此错误

enter image description here

我的错误是什么?为什么说参数类型无效?我是否犯了声明错误?我是新手,但我仍在努力学习这些。详细的解释将很有用

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  int m,n;
  printf("Input the number of Rows: ");
  scanf("%i", &m);
  printf("Input the number of Columns: ");
  scanf("%i", &n);

int *arr=(int*)malloc(m * sizeof(int));
    for(int i=0;i<m;i++)
    {
        arr[i] = (int*)malloc(n*sizeof(int));
    }

    printf("Populate Matrix Row by Row\n--------------------------\n");

    for(int i=0; i<m; i++){
        printf("Row [%i]\n--------\n",i);
        for(int j=0; j<n; j++){
            printf("At Col [%i]= ",j);
            scanf("%i", &*(*(arr+i) + j));
            printf("\n");
        }
    }
printf("[MATRIX]\n------------------\n");
  for(int i=0; i<m; i++){
    for(int j=0; j<n; j++){
      printf("%i ",*(*(arr+i) + j));
    }
    printf("\n");
  }
  printf("------------------\n");

printf("The duplicate value(s) are:\n");
  int temp_index=0;
  for(int i=0; i<m; i++){
    for(int j=0; j<n; j++){
      temp_index=j;
      for(int x=i; x<m; x++){
        for(int y=temp_index; y<n; y++){
           if(j!=y){
             if(*(*(arr+i) + j) == *(*(arr+x) + y)){
             printf("%i in position (%i, %i)\n",*(*(arr+i) + j),x,y);
           }
          }
        }
        temp_index=0;
      }
    }
  }

  free(arr);

  return 0;
}
c arrays memory malloc
1个回答
2
投票
int *arr=(int*)malloc(m * sizeof(int));

*(*(arr+i) + j)*(arr+i)为int,您正试图取消引用int值*(arr+i) + j。我想你会希望

int **arr = malloc(m * sizeof(int*));

如果您使用arr[i][j]而不是*(*(arr+i) + j),将会更清楚,更短。

© www.soinside.com 2019 - 2024. All rights reserved.