C - fgets segfault

I have the following code:

int get_int(void) {
    char input[10];
    fgets(input, 10, stdin); // Segfault here
    return atoi(input);
}

This gives me a segfault where noted. I don’t know what the problem is because I have the following code in another program:

int main(void) {
    char card[17];
    printf("Number: ");
    fgets(card, 17, stdin);
    printf("%s\n", card_type(card));
    return 0;
}

And it works great. I am 100% sure that this is not a segfault on atoi.

Whether this is reproducible by others, I am on Linux amd64 using GCC 4.4.5. He compiled and did not issue any warnings.

Since it was requested, here is the code that calls get_int:

void get_input(int *inputs) { // Stop cluttering up my main
    printf("M spotting F: ");
    inputs[0] = get_int();
    printf("F spotting M: ");
    inputs[1] = get_int();
    printf("F spotting F: ");
    inputs[2] = get_int();
    printf("M spotting M: ");
    inputs[3] = get_int();
}

The code that calls this:

int main(void) {
    int *inputs[4];
    int *heights[4];
    get_input(*inputs);
    get_heights(*inputs, *heights);

    print_bars(*heights);

    printf("M4F  F4M  F4F  M4M\n");
}

And so you have reached the top of the call stack.

+5
source share
1 answer

(, , ) int inputs[4]; , . inputs heights .

4 . *inputs get_int. get_int , , .

+8

All Articles