易百教程

从函数返回指向结构的指针

如何从函数返回指向结构的指针?参考以下示例代码:

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct Date Date;
typedef struct Family Family;

// 函数原型声明
Family *get_person(void);                                        // 输入信息函数
void show_people(bool forwards, Family *pfirst, Family *plast);  // 输出信息函数
void release_memory(Family *pfirst);                             // 释放堆内存

struct Date
{
    int day;
    int month;
    int year;
};

struct Family                          // Family结构体声明
{
    Date dob;
    char name[20];
    char father[20];
    char mother[20];
    Family *next;                        // Pointer to next structure
    Family *previous;                    // Pointer to previous structure
};

int main(void)
{
    Family *first = NULL;                // Pointer to first person
    Family *current = NULL;              // Pointer to current person
    Family *last = NULL;                 // Pointer to previous person
    char more = '\0';                    // Test value for ending input

    while (true)
    {
        printf_s("\nDo you want to enter details of a%s person (Y or N)? ",
            first != NULL ? "nother" : "");
        scanf_s(" %c", &more, sizeof(more));
        if (tolower(more) == 'n')
            break;

        current = get_person();

        if (first == NULL)
            first = current;                 // Set pointer to first Family
        else
        {
            last->next = current;            // Set next address for previous Family
            current->previous = last;        // Set previous address for current
        }
        last = current;                    // Remember for next iteration
    }

    show_people(true, first, last);      // Tell them what we know
    release_memory(first);
    first = last = NULL;
    return 0;
}

Family *get_person(void)
{
    Family *temp = (Family*)malloc(sizeof(Family));

    printf_s("\nEnter the name of the person: ");
    scanf_s("%s", temp->name, sizeof(temp->name));

    printf_s("\nEnter %s's date of birth (day month year); ", temp->name);
    scanf_s("%d %d %d", &temp->dob.day, &temp->dob.month, &temp->dob.year);

    printf_s("\nWho is %s's father? ", temp->name);
    scanf_s("%s", temp->father, sizeof(temp->father));

    printf_s("\nWho is %s's mother? ", temp->name);
    scanf_s("%s", temp->mother, sizeof(temp->mother));

    temp->next = temp->previous = NULL;    // Set pointer members to NULL

    return temp;                           // 返回Family结构的地址
}

void show_people(bool forwards, Family *pfirst, Family *plast)
{
    printf_s("\n");
    for (Family *pcurrent = forwards ? pfirst : plast;
        pcurrent != NULL;
        pcurrent = forwards ? pcurrent->next : pcurrent->previous)
    {
        printf_s("%s was born %d/%d/%d and has %s and %s as parents.\n",
            pcurrent->name, pcurrent->dob.day, pcurrent->dob.month,
            pcurrent->dob.year, pcurrent->father, pcurrent->mother);
    }
}


void release_memory(Family *pfirst)
{
    Family *pcurrent = pfirst;
    Family *temp = NULL;
    while (pcurrent)
    {
        temp = pcurrent;
        pcurrent = pcurrent->next;
        free(temp);
    }
}

执行上面示例代码,得到以下结果:

Do you want to enter details of a%s person (Y or N)?
... ...