0
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>

trying to put fields into my struct but getting segmentation fault when i strcpy them in

typedef struct country {
    char code_name[3];
    char name[44];
    int population;
    float life_expect;
}country[244];

country *data;

int main(void) {


    char c;
    char *ptr;
    int i;
    int temp;
    char buf[512];
    char *token;

    FILE *fptr;
    fptr = fopen("AllCountries.dat", "r");

in here where i call strcpy(data[i]->code_name, token) i get a segmentation fualt. why is that? what am i doing wrong?

    do {
        if (fgets(buf, 512 , fptr)){
            //printf("%s\n",buf);
            token = strtok(buf,",");
            while (token != NULL){          
            token = strtok(NULL, ",");
                if (temp == 0){
                strcpy(data[i]->code_name, token);
                printf("%s, ",token);
                } temp = temp + 1;
            //printf("%s, ",token); 
            //printf("code_name: %s\n", data->code_name);                   
            }
        i++;
        temp = 0;
        }

    }while ((feof(fptr))  != EOF);
    fclose(fptr);
    return 0;
}

the file

115,DZA,Algeria,Africa,Northern Africa,2381741,1962,31471000,69.7,49982,Al-Jazair/Algérie,Republic,Abdelaziz Bouteflika,35,DZ
146,AGO,Angola,Africa,Central Africa,1246700,1975,12878000,38.3,6648,Angola,Republic,José Eduardo dos Santos,56,AO
94,BEN,Benin,Africa,Western Africa,112622,1960,6097000,50.2,2357,Bénin,Republic,Mathieu Kérékou,187,BJ
129,BWA,Botswana,Africa,Southern Africa,581730,1966,1622000,39.3,4834,Botswana,Republic,Festus G. Mogae,204,BW
193,IOT,British Indian Ocean Territory,Africa,Eastern Africa,78,NULL,0,NULL,0,British Indian Ocean Territory,Dependent Territory of the UK,Elisabeth II,NULL,IO
95,BFA,Burkina Faso,Africa,Western Africa,274000,1960,11937000,46.7,2425,Burkina Faso,Republic,Blaise Compaoré,549,BF
Barmar
  • 741,623
  • 53
  • 500
  • 612
jhowe
  • 67
  • 7

2 Answers2

2

You declared a pointer variable data, but never assigned it to point to any memory. You need to do this in main():

data = malloc(sizeof(*data));

But there's no real need for a pointer here, just declare an ordinary variable:

country data;

Actually, I suggest that you don't put the array dimension in the type definition. You should declare the structure type, then declare an array of structures.

typedef struct country {
    char code_name[3];
    char name[44];
    int population;
    float life_expect;
} country;

country data[244];

If you still wanted to use a pointer instead of a regular variable, it would be:

country *data;

then in main() you would do:

data = malloc(244 * sizeof(*data));

You also need to initialize i:

int i = 0;

and

}while ((feof(fptr))  != EOF);

is wrong. feof() returns 1 when EOF is reached, not EOF. It should be:

} while (!feof(fptr));
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thank you! this worked didn't realize what i was doing wrong – jhowe Jan 26 '17 at 01:33
  • 1
    @jhowe You're copying every field of the file into the `code_name` field, but since it's declared `char[3]`, it can only hold 2 characters (the third byte is for the null terminator). So you're writing outside the bounds of the array, which is undefined behavior. – Barmar Jan 26 '17 at 01:33
  • quick question how do i pass an integer from the file to population in the struct i tried this: data[i].population = atoi(token); strcpy(data[i].population, token); but it didnt work – jhowe Jan 26 '17 at 01:46
  • `data[i].population = atoi(token)` should work. You don't need to use `strcpy()` for `population` since it's not a string. – Barmar Jan 26 '17 at 18:52
1

Because the destination, data, which is a pointer to typedef'ed country, is NULL.

strcpy() access the given NULL, and segfaults.

The variable data is a global variable and initialized to NULL in C.

ref: Why are global and static variables initialized to their default values?, Are global variables always initalized to zero in C?

Community
  • 1
  • 1
Yasushi Shoji
  • 4,028
  • 1
  • 26
  • 47