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?

Michael Lilley
  • 407
  • 3
  • 15
  • 1
    Please turn your compiler's warnings to the maximum. You should get a warning about this code. – Sami Kuhmonen May 04 '16 at 03: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 *`. – Tom Karzes May 04 '16 at 04: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`. – Lundin May 04 '16 at 06:26

1 Answers1

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]);
    }
}
fluter
  • 13,238
  • 8
  • 62
  • 100
  • 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](http://stackoverflow.com/questions/36784470/bit-operation-and/36784515#36784515) have been known to happen when they're forgotten. +1 Though. – Serdalis May 04 '16 at 04:27
  • 3
    @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. – Jonathan Leffler May 04 '16 at 04:47
  • @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! – Serdalis May 04 '16 at 04:53
  • 1
    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. – Lundin May 04 '16 at 06:29