-
Display array
how do i display the contents of an array. i got the following code i made, just doesnt display. Says segmentation fault:
Code:
int main(int argc, char * argv[])
{
// Mainline Variable Declarations
FILE * output = stdout;
FILE * input = stdin;
int i;
float array[10];
for(i=1; i<=10; i++)
{
fprintf(output,"Please enter value %i: ",i);
fscanf(input,"%f",&array[i]);
}
for(i=1; i<=10; i++)
{
fprintf(output,"%f ",array[i]);
}
}
there is something wrong with this bit but am not sure:
Code:
for(i=1; i<=10; i++)
{
fprintf(output,"%f ",array[i]);
}
-
Arrays start with 0, not 1.
That means that the array goes from 0 - 9, while you're writing to index 10 at last, thus writing past memory causing undefined behavior(Which might be a seg fault, for example)
-
alright ill try change that next time i have a chance.
thanks
-