How to use fgets() in 2d-arrays (multiple dimension arrays)?
<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

void *salloc(int x){
    char **pointer;
    int i;
    pointer = malloc(sizeof(char)*x);
    if(pointer == NULL){
     exit(-1);
    }
    for(i=0; i&lt;x; i++){
        pointer[i] = malloc(sizeof(char) * 20);
        if(pointer[i] == NULL){
            exit(-1);
        }
    }
return pointer;
}

void Input(int value, char **array){
    for(i = 0; i &lt; value; i++){
        printf("%d ----\n", i);
        fgets(array[i], 20, stdin);
        printf("%d ----\n", i);
    }
}

int main(int argc, char *argv[]){
    char **array;
    int value = 2;
    array = salloc(value);
    Input(value, array);
    return 0;
}
</code></pre>

<p>The general idea, can be that I miss some syntax.
So I want to read in a string with spaces. If I run this for the <strong>value</strong> 2, it will print:</p>

<p>0 ----</p>

<p>0 ----</p>

<p>1 ----</p>

<p>"some string"</p>

<p>and it crashes after I press enter.
If I do this with <strong>value</strong> 1:
it immediately crashes.
However if I replace fgets() with: </p>

<pre><code>    scanf("%s", array[i]);
</code></pre>

<p>it works (except for the spaces).</p>

<p>So how does fgets() work in 2d-arrays? </p>

<p>Because I get it to work in 1d-arrays. And for some reason I can print 1d-arrays from row 2 when the array only has 2 rows, so it should only be able to print from rows 0 and 1 right?</p>