0

There is a specific csv file containing lines in a specific format, and I defined a struct of it.

typedef struct {
    int passengerId;
    int survival;   // survived = 1, dead = 0
    enum PClass pclass; // 1st, 2nd, 3rd class
    char* name;
    enum Sex sex;
    float age;  
    int sibsp;  //number of siblings
    int parch;  //number of family members
    char* ticket;   //ticket number
    double fare;    
    char* cabin;    
    char* embarked; 
}SUVData;

and this struct is declared in main() with a double pointer format, initial size 1 - for implementation of 2-dimensional array :

int main() {
SUVData** data = (SUVData**)calloc(1, sizeof(SUVData*));
ReadData(data, "titanic.csv");

return 0;

}

and this is given as a parameter to the reading data function :

void ReadData(SUVData** dataset, char* filename) {
    FILE* fp;
    char line[1024];    
    int i = 0;

    // temporary variables
    int tempPassengerId;
    int tempSurvival;
    int tempPClass;
    char* tempName = (char*)malloc(sizeof(char) * 100);
    char* tempSex = (char*)malloc(sizeof(char) * 7);
    float tempAge;
    int tempSibsp;
    int tempParch;
    char* tempTicket = (char*)malloc(sizeof(char) * 20);
    double tempFare;
    char* tempCabin = (char*)malloc(sizeof(char) * 10);
    char* tempEmbarked = (char*)malloc(sizeof(char) * 2);

    fp = fopen(filename, "r");

    if (fp == NULL) {
        printf("file open error\n");
        return;
    }

    printf("\nFile loaded\n");

    while (fgets(line, 1024, fp)) {
        line[strlen(line) - 1] = '\0';
        printf("test line : %s\n", line);   

        // Format in temp variables
        int t = sscanf(line, "%d,%d,%d,%[^,],%[^,],%f,%d,%d,%[^,],%lf,%[^,],%s", &tempPassengerId, &tempSurvival, &tempPClass, tempName, tempSex, &tempAge, &tempSibsp, &tempParch, 
            tempTicket, &tempFare, tempCabin, tempEmbarked);
        
        // Re-arrange in dataset (where I'm having problem)
        (*(*(dataset + i))).passengerId = tempPassengerId;
        printf("test Id : %d\n", (*(*(dataset + i))).passengerId);


        i++;
    }
    
    fclose(fp);
}

I was planning on assigning temporary variables inside struct SUVData, using code like this(as written above) : (*(*(dataset + i))).passengerId = tempPassengerId;

Tried to test if it's working, but no.

SinonOW
  • 101
  • 2
  • [How do I work with dynamic multi-dimensional arrays in C?](https://stackoverflow.com/a/12805980) – 001 Dec 08 '20 at 14:12
  • Please [edit] your question and create a [mre]. Add a `main` function that calls `ReadData` and if necessary prints the result. Show some example input and the actual output you get from this input and the expected output you would like to get. If you get errors or warnings from the compiler, show these as well. – Bodo Dec 08 '20 at 14:14
  • Never try to write into a dynamic object unless you know where it has been allocated. Here you show that you only allocated a single pointer, but never that you allocated the `SUVData` objects. And when this will work think about the second rule: always deallocate any object that you allocated.... It may not be sexy, but dynamic data handling in C is at a very low level. – Serge Ballesta Dec 08 '20 at 14:18
  • @SergeBallesta wow. I can't believe I just made a such dumb mistake. Really appreciate for your comment.. – SinonOW Dec 08 '20 at 14:23
  • @JohnnyMopp For more details, there's also [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) – Lundin Dec 08 '20 at 15:02

0 Answers0