Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use a pointer in a structure

If you enter 1 for the first destination, 2 for the date, 3 for the second, 4 for the date, 5 for the last destination, and 6 for the date, 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. )

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

#pragma warning(disable:4996)

void fillFlightInfo(struct FlightInfo* db, char* destinationStr, char* dateStr);

void printFlightInfo(struct FlightInfo* db);

void clearCR(char* buf);


struct FlightInfo
{
    char* destination;
    char* date;
};

int main(void)
{
    struct FlightInfo* pData = NULL;
    char destinationStr[30] = "";
    char dateStr[30] = "";


    pData = (struct FlightInfo*)malloc(4 * sizeof(struct FlightInfo));


    struct FlightInfo* db = pData; //the beginning address


    if (pData == NULL)
    {
        printf("Out of memory\n");
        return -1;
    }


    for (int i = 1; i < 4; i++)
    {
        fillFlightInfo(db + i, destinationStr, dateStr); // (db + i)

        printf("%d %35s %35s\n", i, (db + i)->destination, (db+i)->date);
        db++;
    }

    printf("\n");
    db = pData;

    printFlightInfo(db);

    if (pData != NULL)
    {
        free(pData);
    }

    return 0;
}

void fillFlightInfo(struct FlightInfo* db, char* destinationStr, char* dateStr)
{
    printf("Enter a flight destination:   ");
    fgets(destinationStr, sizeof destinationStr, stdin);
    db->destination = destinationStr;
    clearCR(db->destination);

    printf("Enter a flight date:   ");
    fgets(dateStr, sizeof dateStr, stdin);
    db->date = dateStr;
    clearCR(db->date);

}

void printFlightInfo(struct FlightInfo* db)
{
    for (int i = 1; i < 4; i++)
    {
        printf("%d %35s %35s\n", i, (db + i)->destination, (db + i)->date);
        db++;
    }
}

void clearCR(char* buf)
{
    char* whereCR = strchr(buf, '\n');
    if (whereCR != NULL)
    {
        *whereCR = '\0';
    }
}


/*
*** input ***
1
2
3
4
5
6


*** output ***
1 5 6
2 5 6
3 5 6
*/

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading