D4RTHW1MP
D4RTHW1MP

Reputation: 23

How Can I get input in arrays in C?


    #include <stdio.h>
    #include <math.h>
    int main()
    {
        int numbers[5];
        printf("Enter your first number:%d");
        scanf("%d", numbers[0]);
        printf("Enter your second number:%d");
        scanf("%d", numbers[1]);
        numbers[3]=numbers[0]+numbers[1];
        printf("Your desired result is:%d",numbers[3]);
        return 0;
    }

I seem to find no problem in the code but it won't even let me input numbers in the array I declared

Upvotes: 1

Views: 66

Answers (1)

Serve Laurijssen
Serve Laurijssen

Reputation: 9733

The issue is in the second argument of scanf.

scanf("%d", numbers[0]);

it expects an address of where to write your input to but you are passing the value of numbers[0]. So scanf will write to whatever is in numbers[0] which is an int, not a pointer.

Take the address of numbers[0] by changing it to:

scanf("%d", &numbers[0]);

Upvotes: 1

Related Questions