-1

Can you get input in double pointer after scanf? Is this wrong? I have malloced the array in the main function.

void input(int **array, int x, int y){
        int s,t;

        printf("array element:\n");
        for(s = 0; s < x; s++){
                for(t = 0; t < y; t++){
                       scanf("%d", array[s][t]);
                }
        }
} ```
James
  • 1
  • 1
    `**array` it is not the array – 0___________ Feb 05 '20 at 17:48
  • 1
    Yes, but `array[s][t]` is an `int`, and `scanf` needs the address of the object, not the object itself, so use `&array[s][t]` (an `int *`). – Ian Abbott Feb 05 '20 at 17:49
  • 2
    This is just a typo, you're missing `&`. – Marco Bonelli Feb 05 '20 at 17:49
  • 1
    Compile with `gcc -Wall -Wextra` and you'll see the error. – Marco Bonelli Feb 05 '20 at 17:51
  • 1
    What exactly did you `malloc`? If it was space for a single 2D array of `int`, then the `int **array` parameter will be incompatible with it. It wants a (pointer to first element of an) array of `int *`, with each `int *` pointing to an array of `int`. – Ian Abbott Feb 05 '20 at 17:54
  • @MarcoBonelli: It does not appear to be a typographical error. It appears to be a conceptual error. The fact that correct code can be obtained by inserting a single character does not mean the errant code was caused by failing to type an intended character rather than by a lack of understanding by the author. – Eric Postpischil Feb 05 '20 at 18:42
  • @IanAbbott: That is not relevant; the question does not complain of a compiler reporting a type mismatch in calling the function, and `&array[s][t]` would serve in the `scanf` inside the function regardless of whether the proper parameter type were `int **` or `int (*)[dimension]`. – Eric Postpischil Feb 05 '20 at 18:44

1 Answers1

0

scanf must be passed the address of the object to be written to. If you want to scan a value into array[s][t], you must pass its address, &array[s][t]. Replace scanf("%d", array[s][t]); with scanf("%d", &array[s][t]);.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312