0

If you enter 1 for the first id, 2 for the password, 3 for the second, 4 for the password, 5 for the last destination, and 6 for the d, the results are all displayed as 5 and 6.

I'd appreciate your help.

I wonder that the output values are all 5 and 6. ( You must write a pointer inside the structure. )

void game(struct gameInfo *login, char *id, char *password) {
    printf("game dts:   ");
    fgets(id, sizeof id, stdin);
    login->dts = id;
    cl(login->dts);

    printf("game password:   ");
    fgets(password, sizeof password, stdin);
    login->password = password;
    cl(login->password);
}

void printGame(struct gameInfo *login) {
    for (int i = 1; i < 4; i++) {
        printf("%d %35s %35s\n", i, (login + i)->dts, (login + i)->password);
        login++;
    }
}

int main(void) {
    for (int i = 1; i < 4; i++) {
        gameInfo(login + i, id, password);
        printf("(login + i)-> dst, (login + i)->password);
        login++;
    }
}
  • First decide which language you're programming in. C and C++ are two *very* different languages, and with C++ your code should look *very* different. – Some programmer dude Jan 19 '23 at 13:36
  • As for your problem, you have *one* "destination" string, and *one* "date" string, and you make all the pointers point to these single strings. Consider using arrays in the structure as well, and copy the strings instead. – Some programmer dude Jan 19 '23 at 13:38
  • Ask yourself a very simple question: you are reading multiple records, you are using the same exact set of buffers for each record, how do you expect to end up with different records, which are read into the exact same set of buffers? Once you figure out the answer to this question, everything will become clear. – Sam Varshavchik Jan 19 '23 at 13:39
  • 1
    On a different note, `(db + i)->destination` is *exactly* the same as `db[i].destination`. The latter (with array indexing) is easier to read, understand, maintain, and also less to write. – Some programmer dude Jan 19 '23 at 13:39
  • 1
    All your pointers in all 3 structs eventually point to the same memory address of local function variables `char destinationStr[30]` and `char dateStr[30]`. You read something into those char arrays using fgets. Then copy the pointer to the first struct variable, then you read into the same memory again and copy the pointer into the second struct. Because the first struct still points to the same memory, they all have the same "value". After reading into the buffer you need to allocate memory for each of structs' `char*` and copy from buffer to it in order to persist the data. – theemee Jan 19 '23 at 13:49
  • `struct FlightInfo` must be declared prior to declaring either of the prototypes for `printFlightInfo` or `fillFlightInfo` as both functions include the `struct` – ryyker Jan 19 '23 at 13:49
  • Thank you all for your help. I think we've got a problem. –  Jan 19 '23 at 13:50
  • 1
    Another problem is with your loops: you add the `db + i` while also incrementing `db++` in the end of the loop, which offsets the array twice per iteration. You need to use either pointer arithmetic or indexing. I would suggest using indexes – theemee Jan 19 '23 at 13:55
  • Aside: This: `pData = (struct FlightInfo*)malloc(4 * sizeof(struct FlightInfo));` can be written as `pData = malloc(4 * sizeof(struct FlightInfo));` in C, [and should be](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – ryyker Jan 19 '23 at 13:56
  • As a simple way to visualize the problem here, bring out some paper and a pen. Draw four boxes on the paper, and label them `date1` to `date4`. These represents the `date` member of the structures in your array. Then draw another box, and label it `dateStr` This represents `dateStr`. Now draw arrows from all the `date1`, `date2`, etc. boxes to the single `dateStr` box. This is how the pointers are working. – Some programmer dude Jan 19 '23 at 13:57

1 Answers1

3

A pointer points to another variable. All your FlightInfos' dates point to the dateStr variable in main, and all their destinations point to the destinationStr variable in main. So when you print what they point to, you're printing the dateStr and destinationStr variables in main.

To solve it you would have to create new "variables" to hold each FlightInfo's date and destination. The obvious way to do it would be to make them arrays instead of pointers, but I guess the purpose of the exercise is to learn pointers - you can use malloc to allocate some memory space for long-term usage, and then access it by pointer. (Don't try to store the date and destination in local variable arrays inside fillFlightInfo, since you'll get a similar problem - when it returns, the local variables are deleted and the space will be reused for the next call)

user253751
  • 57,427
  • 7
  • 48
  • 90