Display array

This is a discussion on Display array within the C Programming forums, part of the General Programming Boards category; how do i display the contents of an array. i got the following code i made, just doesnt display. Says ...

  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
    478
    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, 09: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-12-2004, 12:15 AM
  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, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21