Man Man
Man Man

Reputation: 11

Segmentation Fault on creating an array with user's input in C

I am trying to implement a simple program, asking users to input the array length and numbers to fill up the array, then print out the array with printf.

If I use Method 1 and declare the array at the very beginning, the program will print out successfully but shows segmentation fault at the end.

If I use Method 2, declare the array after the user input numbersLength, there will be no segmentation fault.

What's the difference and why there is segmentation fault in method 1?

Any help would greatly be appreciated!

Method 1

#include<stdio.h>
int main()
{   
    int numbers[] = {}; 
    int numbersLength;
    printf("numbersLength: \n");
    scanf("%d", &numbersLength);
    for (int i=0 ; i < numbersLength; i++){
        printf("number: \n");
        scanf("%d", &numbers[i]);
    }
    for (int i=0 ; i < numbersLength; i++){
        printf("%d \n", numbers[i]);
    }
}
numbersLength: 
5
number: 
1
number: 
2
number: 
3
number: 
4
number: 
5
1 
2 
3 
4 
5 
Segmentation fault

Method 2

#include<stdio.h>
int main()
{   
    int numbersLength;
    printf("numbersLength: \n");
    scanf("%d", &numbersLength);
    int numbers[numbersLength]; 
    for (int i=0 ; i < numbersLength; i++){
        printf("number: \n");
        scanf("%d", &numbers[i]);
    }
    for (int i=0 ; i < numbersLength; i++){
        printf("%d \n", numbers[i]);
    }

}

Upvotes: 0

Views: 679

Answers (2)

user3629249
user3629249

Reputation: 16540

method 1 results in:

gcc -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled2.c" -o "untitled2.o"

untitled2.c: In function ‘main’:

untitled2.c:4:21: warning: ISO C forbids empty initializer braces [-Wpedantic]
     int numbers[] = {};
                     ^
untitled2.c:4:9: error: zero or negative size array ‘numbers’
     int numbers[] = {};
         ^~~~~~~
Compilation failed.

so method 1 is NOT valid C code

method 2 uses the VLA (Variable Length Array) feature of C so does produce an array of the right size

Upvotes: 0

Prince Wilson
Prince Wilson

Reputation: 41

The length of the array was mentioned while declaring the array, that is why you didn't get a segmentation fault in the second method.

Segmentation fault means that you are accessing a part of the memory without claiming it to be yours.

You need not ponder over the reason why you got that error in the first method and why not in the second method also because according to the syntax of declaring an array you must always mention the arrays length.

Your syntax was wrong in the first method and that's why you got that error. That is all.

You mentioned that the segmentation fault error occurred after the numbers where printed right? But check my output when I compiled the same program using CodeBlocks:

numbersLength:
3
number:
1
number:
2
0
3
7869408

Process returned 0 (0x0)   execution time : 3.687 s
Press any key to continue.

Here I didn't get any error but I did not get the desired output either.

So I think you'll get surprisingly wrong results if you don't follow the syntax.

You can declare an array without the length also but you must also initialize it then and there itself.

For example:

int numbers[]= {1, 2, 3, 4}

Here the compiler will automatically store the array length as four. But you cannot use this method if you want the input from the user.

Upvotes: 2

Related Questions