The Derfer said:
This code causes a segmentation fault when I run using ./a.out
It compiles fine though.
#include <stdio.h>
int main()
{
int ix;
const int array_size = 10;
int ia[array_size];
for (int ix= 0 ; ix < array_size ; ++ix );
ia[ix]=ix;
printf("%d","%d",ix,ia[ix]);
}
Apart from the other errors, note that ia is actually a VLA (variable
size array). An object declared "const" is not a constant in C; "const"
merely means that it's read-only, not that its value is determined at
compile time. For example:
const int r = rand();
is valid.
In practice, the compiler will probably treat it the same, or nearly so,
as if the size were constant, but it might be better to actually
make it constant, either:
#define ARRAY_SIZE 10
or
enum { array_size = 10 };
A minor point: "int main()" should be "int main(void)".
Proper indentation is important. Indentation should reflect the
structure of your program. If you have the "indent" tool, try
running your code through, for example, "indent -kr". That would
have made it more obvious that the statements following the for
loop are not controlled by it. (Read the documentation; it creates
a backup and clobbers the input file, which is somewhat unusual
behavior for such a tool.)