problem passing to array

This is a discussion on problem passing to array within the C Programming forums, part of the General Programming Boards category; I am having some serious issues trying to pass the elements into the array at the end of the code ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    problem passing to array

    I am having some serious issues trying to pass the elements into the array at the end of the code segment. but i dont understand why, it prints them just fine but for some reason instead of printing them i want them into an array, but i just get a seg fault when i try to assign them to the array. it seems like a simple problem that i am just not seeing :/

    so this is what i have so far...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main ( int argc, char *argv[] )
    {
        if ( argc != 2 )
        {
            printf( "usage: %s filename", argv[0] );
        }
        else 
        {
            FILE *file = fopen( argv[1], "r" );
            
            if ( file == 0 )
            {
                printf( "Could not open file\n" );
            }
            else 
            {
                int x;
                int i = 0;
                
                arr[5]   //right now im just assuming the size of the array is 5 cuz thats my test file size
                  
                while  ( ( x = fgetc( file ) ) != EOF )
                {
                   // printf( "%c", x );
                      arr[i] = x;
                      i++;
                }
             
            }
            fclose( file );
        }
    }
    if it helps the test file looks like this:
    8.3945
    4.3495
    2.3598
    5.0382
    7.9384

  2. #2
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    and what is the type of arr?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    The file contains 5 floating point numbers plus assorted new-lines? Then reading a character at a time will certainly overflow the array of 5 elements.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    sorry, the array is of type int. How do I get around it overflowing then?, should I use malloc, iv just been playing around with this for hours, so I'm really lost and confused. :/

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by helg View Post
    sorry, the array is of type int. How do I get around it overflowing then?, should I use malloc, iv just been playing around with this for hours, so I'm really lost and confused. :/
    We're at the super market, to get a dozen watermelons. You ask the produce guy for a container to put the watermelons in, and he hands you an egg carton.

    That's what you're doing with arr[]. If you want arr[] to hold floats, then make it a float data type array, NOT an int array. Drop fgetc() also, since it doesn't handle floats.

    You don't need malloc(). You need to declare arr[] as an array of type float. You need to match the data type going into the array, with the data type OF the array.
    Last edited by Adak; 09-27-2010 at 03:17 PM.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    i figured it out, not only was i using the wrong type for the array, i was reading it character by character because i was using fgetc. so i switched the array type to float and used fscanf and it works now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting array problem :)
    By BEST in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2009, 01:57 PM
  2. Passing an array of strings to a function.
    By dazman19 in forum C Programming
    Replies: 10
    Last Post: 09-16-2009, 07:32 PM
  3. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 12:53 PM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 12:35 PM

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