0

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]);
    }

}
3
  • 2
    How big do you think the array numbers is ? Any text book or online course will cover the basics. You can't learn C by trial and error. Commented Jul 17, 2020 at 4:14
  • 4
    int numbers[] = {}; That declares an array of 0 size. So accessing any elements in that array results in undefined behaviour. Arrays in C do not automatically grow in size. Commented Jul 17, 2020 at 4:32
  • 5
    How that compiled is a bit of a mystery int numbers[] = {}; is not valid C "error: zero or negative size array" Commented Jul 17, 2020 at 4:33

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.