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;
6
  • What about (*arr)[i][j] ? Commented Mar 19, 2014 at 14:14
  • Please show a simple example of input format. Commented Mar 19, 2014 at 14:18
  • @timrau 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 Commented Mar 19, 2014 at 14:29
  • @timrau yes i included <stdlib.h> as well Commented Mar 19, 2014 at 14:34
  • 1
    Unrelated to the actual question, but don't cast malloc. Commented Mar 19, 2014 at 14:49

1 Answer 1

2

There is a severe typo:

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

Should be

  arr[i] = malloc(n * sizeof(int));
Sign up to request clarification or add additional context in comments.

6 Comments

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
Since the way you call malloc looks fine, we need your input format to diagnose the problem.
Any message when your debugger stopped there? Is it waiting for your input?
error waa like: Unhandled exception at 0x0f75effe in spiral.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.