Thread: Passing Structs Into An Array Of Structs.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    15

    Passing Structs Into An Array Of Structs.

    Here I have a C program that is supposed to pass structs that it reads from a .csv file to get the fields for the individual structs. Then it should pass the filled struct into the array. But it's not putting the information in the right slots in the array. I'm pretty new to C. I generally work with C#. So I'm not used to pointers and addresses and the like. I might have done something wrong there. If someone can point me in the right direction that would be great!

    Thanks!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <string.h>
    
    
    typedef struct {
        //COUNTRY ID, COUNTRY NAME, POPULATION, LIFE EXPECTANCY, AND DATE OF FOUNDING
        char *id;
        char *name;
        int population;
        float lifeExpectancy;
        int year;
    }CountryData;
    CountryData FormatData(char *data);
    int main()
    {
        int         fr                      =   open("SampleCountriesInput.csv", O_RDONLY);
        int         inputSize               =   200;
        int         i                       =   0;
        int         j                       =   0;
        char        line[inputSize];
        char        buff[1];
        ssize_t     n;
        CountryData countries[50];
        CountryData country;
        do
        {
            n = read(fr,buff,1);
            if(buff[0] == '\n')
            {
                country = FormatData(line);
                //countries[j] = FormatData(line);
                strncpy(&countries[j].id, &country.id,3);
                strncpy(&countries[j].name, &country.name, 18);
                countries[j].population = country.population;
                countries[j].year = country.year;
                countries[j].lifeExpectancy = country.lifeExpectancy;
                j++;
                i=0;
            }
            else
                line[i] = buff[0];
            i++;
        }while( n != 0);
        j--;
        while(j!=0)
        {
            printf(countries[j].name);
            j--;
        }
        return 0;
    }
    
    CountryData FormatData(char *data)
    {
        CountryData country;
        char *token[20];
        int j, i = 0;
        token[0] = strtok(data, ",");
        while(token[i] != NULL)
        {
            i++;
            token[i] = strtok(NULL, ",");
        }
        country.id = token[1];
        country.name = token[2];
        country.lifeExpectancy = atof(token[8]);
        country.population = atoi(token[7]);
        country.year = atoi(token[6]);
        return country;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    strncpy(&countries[j].id, &country.id,3);
    strncpy(&countries[j].name, &country.name, 18);
    The problem here is that your countries[].id (and name) are just pointers, and have not been allocated any space.


    country.id = token[1];
    country.name = token[2];
    This "works" because you´re setting a pointer to somewhere in your input string (data).


    Start with char id[4] (for example), and use strcpy() to copy the data from one place to another.

    I see you use strncpy(), which is good at limiting the data, but it does NOT guarantee the result is a proper C string (with a /0 at the end).

    If all your data at the moment is a valid length, then get something working with strcpy() first.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    // hard way
           if(buff[0] == '\n')
            {
                country = FormatData(line);
                //countries[j] = FormatData(line);
                strncpy(&countries[j].id, &country.id,3);
                strncpy(&countries[j].name, &country.name, 18);
                countries[j].population = country.population;
                countries[j].year = country.year;
                countries[j].lifeExpectancy = country.lifeExpectancy;
                j++;
                i=0;
            }
    Code:
      // easy way
           memcpy(&countries[j],&country,sizeof(countrydata));

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Or better still:
    Code:
    countries[j] = country

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Inserting new member into array of structs
    By cathym in forum C Programming
    Replies: 6
    Last Post: 04-17-2005, 07:10 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Passing an array of structs to a function
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-03-2001, 12:02 PM