0

i'm trying to solve a issue in my code... Im a litle bit new in C program, sorry if i cant explain my issue very well, but its my first time that i put a question on stackoverflow.

So i have this code in my program:

typedef struct contact {
    int iD;
    int age;
    char phone1[15];
    char phone2[15];
    char contactFirstName[20];
    char contactLastName[20];
    char contactCompany[30];
    char contactAddress[40];
    char contactCity[20];
    char contactState[20];
    char contactCountry[20];
    char contactPostalCode[20];
    char contactEmail[30];
    char contactWeb[50];
}contact;

int lines=0;
void printArray(contact *array, int n);
char* sortFieldMenu();
char* sortTypeMenu();
void mainMenu();
char* searchFieldMenu();
contact* readFile(char*path);

int main(void){
    system("play -n synth 0.1 sin 1880 | echo -e ""\a"" ");
    char* filePath = "contact.csv";

    contact* inArray = (readFile(filePath));
    printf("\n Debug First Name: %p \n",inArray[1].age); 

    printf("Debug.......");
    return 0;
}

contact* readFile(char *path){
    int i, count;
    char ch;
    FILE* fp = fopen(path, "r");
    if (fp == NULL) {
        fprintf(stderr, "\n Erro ao abrir o ficheiro de !\n");
        exit(1);
    }
    while(!feof(fp)){
        ch = fgetc(fp);
        if(ch == '\n'){
            lines++;
        }
    }
    printf("Number of Lines: %d \n", lines);
    contact inArray[lines];
    rewind(fp);

    for(i=0;i<=lines-1;i++){

        count++;
        fscanf(fp, "%d,%[^,],%[^,],%d,%[^,],%[^,],%[^,],%[^,],%[^,],%,],%
               [^,],%[^,],%[^,],%s\n", &inArray[i].iD, 
               inArray[i].contactFirstName,                             
               inArray[i].contactLastName, &inArray[i].age, 
               inArray[i].contactCompany, inArray[i].contactAddress, 
               inArray[i].contactCity, inArray[i].contactState,                 
               inArray[i].contactCountry,
               inArray[i].contactPostalCode,
               inArray[i].phone1, 
               inArray[i].phone2,inArray[i].contactEmail,
               inArray[i].contactWeb);
    }

    printArray(inArray, lines);
    fclose(fp);
    return inArray;
}

Im trying to access(print) an inArray element on the main function. I know that i cant return an array from a function, just the pointer to that array. The main goal is to read an .csv file and put the data in an array of structs (inArray) initialize on that function, and return a pointer to that array.

Im trying to access tho that array in the main function, and print an element( inArray[1].age), but something is wrong.

Any help will be apeciated.

mch
  • 9,424
  • 2
  • 28
  • 42
  • Take a look at http://stackoverflow.com/q/5431941/3684343 – mch Apr 12 '17 at 20:01
  • inArray is a function-local variable. It is on the stack during the function execution. After "return" is stops existing. Returning a pointer to it is a path to hell. That is different if you use dynmically allocated memory (`malloc()`), even if it gets allocated inside the function. – Yunnosch Apr 12 '17 at 20:10
  • By the way your way of asking a question is already quite good. I neveertheless recommend to take the tour and get the badge for it. Some answerers here have something of a preconception against askers who do not have that badge. http://stackoverflow.com/tour – Yunnosch Apr 12 '17 at 20:13
  • Also, it looks like your format string is incomplete: `,%,],` – 001 Apr 12 '17 at 20:17

0 Answers0