2

I'm new to C, coming from Java.

I made the following trivial program that compiles fine, but nano throws me a Segmentation Fault whenever I run it. The point of it is to traverse through the array and have it print out each element on a separate line.

int main()
{
    int array[5] = {1, 2, 3, 4, 5};
    int i = 0;
    for (i = 0; i < sizeof(array); i++)
    {
            puts(array[i]);
    }
}

What am I doing wrong?

3
  • 1
    Please turn your compiler's warnings to the maximum. You should get a warning about this code. Commented May 4, 2016 at 3:59
  • 3
    sizeof gives the size of the object, in bytes. That is not the same as the number of elements in an array (unless the size of an element is 1 byte). To get the number of elements, you must divide the value by the size of a single array element. Also, you can't pass an int to puts, which expects a string, i.e. a char *. Commented May 4, 2016 at 4:02
  • This isn't even valid C. If this compiled, it means your compiler is misconfigured. In case of GCC for example, make sure to use it as a C compiler: gcc -std=c11 -pedantic-errors. Commented May 4, 2016 at 6:26

1 Answer 1

8

First, puts takes an null terminated strings, not an integer. Second, to determine the number of elements in the array, use sizeof(array)/sizeof(array[0]), because sizeof(array) is the total number of bytes of the array. Third, use int main(void) for standard C. Try this:

int main(void)
{
    int array[5] = {1, 2, 3, 4, 5};
    int i = 0;
    for (i = 0; i < sizeof(array) / sizeof(array[0]); i++)
    {
            printf("%d\n", array[i]);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Good answer, but minor nitpicks, You forgot to return 0, and its always a good idea to put brackets around operations on either side of a comparison since Strange Things have been known to happen when they're forgotten. +1 Though.
@Serdalis: In C99 and C11, if execution falls off the end of main(), it is equivalent to return 0;. I don't like this — but it agrees with what C++98 decided (and I don't like that decision either), so that isn't wholly fair. I don't see any relevant problems with missing parentheses, either — it is not unreasonable to expect people to be vaguely familiar with the table of operator precedences, and there are no bitwise operations here to confuse things.
@JonathanLeffler You're right, I didn't realise the return 0 bit though, its probably just because I've been forced to use C89 all my life, all this new flim-flammery is quite odd!
The empty parenthesis of a function taking no parameters is allowed by the current standard, but is marked as an obsolete feature. If you write such code, it might not work in the future.

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.