0

I am trying to traversing an array into struct in C, but I got the error: error: expected identifier befoe '(' token

My Structs

typedef struct book {
    int         book_code;
    char        title[80];
    char        author[80];
    InfoStudent status[2];
} Book;

typedef struct info_student {
    char    status_char;
    char    student_code[7];
    int     day_get;
    int     month_get;
    int     day_dev;
    int     month_dev;
} InfoStudent;

My function to save the data with error:

void create_book(Book *pbk) {
    pbk->book_code= 1;
    printf("Type the title:");
    gets(pbk->title);
    fflush(stdin);
    printf("Type the author:");
    gets(pbk->author);
    fflush(stdin);
    int i;
    for (i=0; i<2; i++) {
        pbk->(status+i)->day_get = -1; // Error line
        pbk->(status+i)->day_dev = -1;
        pbk->(status+i)->month_get = -1;
        pbk->(status+i)->month_dev = -1;
        pbk->(status+i)->student_code = '';
        pbk->(status+i)->status_char ='F';
    }
}

I am trying to set the status+0 values and status+1 values, using this for loop to optimize my writing code. I don't want use the "[" "]" to access the values.

r1ddax
  • 11
  • 2
  • 2
    `pbk->status[i].day_get = -1;` – Zakk Nov 21 '22 at 13:03
  • And that `fflush(stdin);` is [undefined behaviour](https://stackoverflow.com/questions/2979209/using-fflushstdin). – Zakk Nov 21 '22 at 13:04
  • @Zakk, I do not want to use square brackets..... – r1ddax Nov 21 '22 at 13:05
  • What is the reason not to use square brackets? Using array indexing is often easier to read compared with pointer arithmetics. The task of optimizing your code should be left to the compiler unless you have a specific reason to assume this is not sufficient. – Bodo Nov 21 '22 at 13:08
  • @Bodo yes, the reason is because I am learning new ways to do some tasks, it is a study and I have tried with square brackets before and worked, now I am trying this way. – r1ddax Nov 21 '22 at 13:11
  • You should not use `gets`. This function is deprecated since C99 and was removed from the standard since C11. See https://stackoverflow.com/a/4309845/10622916 and https://en.cppreference.com/w/c/io/gets – Bodo Nov 21 '22 at 13:12
  • Also those `gets()` calls are problematic. I suggest you find a better C book. – Zakk Nov 21 '22 at 13:12
  • @r1ddax `(pbk->status+i)->day_get = -1;` – Zakk Nov 21 '22 at 13:13
  • @Zakk, that's it. thanks for helping. I read the other articles and improve my study case. Thanks. – r1ddax Nov 21 '22 at 13:42

0 Answers0