-1

SO i was solving this problem and i am getting this error

enter image description here

What is my mistake here?Why is it saying invalid argument type?Is there any declaration mistake I made? I am a newbie still I am trying hard to learn these. A detailed explanation will be useful

#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;
}
luciferjack
  • 107
  • 2
  • 12
  • Error message says the result of the expression `*(arr + i) + j` is an `int`, and using `(*1234)` isn't acceptable. Consider that if a single-dimension malloc is an `int*` (pointer to 'array of' integers), then a double dimension should be `int**` (pointer to an 'array of' pointers to 'array of' integers; where 'array of' is used loosely). – user2864740 Jun 01 '20 at 19:23
  • I see you are still using the syntax commented on in your [previous question](https://stackoverflow.com/questions/62116373/c-program-to-find-the-duplicate-values-from-an-m%c3%97n-matrix-using-malloc-and-fre#comment109861101_62116373). It is not easy to follow code written like this. – Weather Vane Jun 01 '20 at 19:25
  • It's it really easier to make a screenshot, crop the image and paste it in the question instead of coping and pasting the text? – 273K Jun 01 '20 at 19:33
  • What changes should have been made to this code? – luciferjack Jun 01 '20 at 19:34
  • Sorry, why do you write `*(*(arr + i) + j)`, instead of `arr[i][j]` ??? Why don't you write `j[i[arr]]` (which happens to be the same thing) to make people get crazy reading at your code? – Luis Colorado Jun 02 '20 at 21:27

1 Answers1

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

*(*(arr+i) + j): *(arr+i) is int, you are trying to dereference the int value *(arr+i) + j . I assume you would wish

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

It would be more clear and shorter if you had used arr[i][j] instead of *(*(arr+i) + j).

273K
  • 29,503
  • 10
  • 41
  • 64
  • This seems to be an example of [why not to cast the result of `malloc`](https://stackoverflow.com/q/605845/10077). Even if you get it wrong, the compiler still doesn't care. So why do it? – Fred Larson Jun 01 '20 at 19:30