1

The scanf statement is giving me trouble. I have tried &arr[i][j] and (arr+i)+j in place of *(arr+i)+j. However, this statement is still giving problems. Here is my code:

int **arr, m, n, i, j;
scanf("%d%d", &m, &n);
arr = (int **) malloc( m * sizeof(int *) );

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

for(i = 0; i < m; i++)
  for(j = 0; j < n; j++)
    scanf("%d", *(arr + i) + j); //this statement

for(i = 0; i < m; i++) {
  for(j = 0; j < n; j++) {
    printf("%d ", *(*(arr + i) + j));
  printf("\n");
}

getch();
return 0;
double-beep
  • 5,031
  • 17
  • 33
  • 41
gj1103
  • 127
  • 2
  • 10

1 Answers1

2

There is a severe typo:

  arr[m] = (int *) malloc(n*sizeof(int));

Should be

  arr[i] = malloc(n * sizeof(int));
timrau
  • 22,578
  • 4
  • 51
  • 64
  • i tried &arr[i][j] as well, its not working. my debugger stops at this statement. Is there something wrong with the way i m using malloc? looks like i m using the adresses in a wrong way – gj1103 Mar 19 '14 at 14:22
  • Since the way you call `malloc` looks fine, we need your input format to diagnose the problem. – timrau Mar 19 '14 at 14:24
  • Any message when your debugger stopped there? Is it waiting for your input? – timrau Mar 19 '14 at 14:24
  • error waa like: Unhandled exception at 0x0f75effe in spiral.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd. – gj1103 Mar 19 '14 at 14:25
  • firstly i want user to enter no of rows and column into m and n variables respectively, then i tried allocating memory for them. then user will enter numbers input sample: 3 4 1 2 3 4 5 6 7 8 9 10 11 12 where 3 and 4 are no of rows and columns – gj1103 Mar 19 '14 at 14:30
  • @user3437973 Edited. Found the actual root cause. – timrau Mar 19 '14 at 14:32