Thread: Display array

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

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

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    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)

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    alright ill try change that next time i have a chance.
    thanks

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    ya worked thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Array won't display new values?
    By Kespoosh in forum C++ Programming
    Replies: 9
    Last Post: 03-17-2003, 09:22 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM