易百教程

结构体指针作为结构成员

任何指针都可以是结构体的成员。允许指向相同类型结构的指针作为结构体的成员。

示例代码

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

typedef struct Dog Dog;            // Define Dog as a type name

struct Dog                         // Structure type definition
{
    int age;
    int height;
    char name[20];
    char father[20];
    char mother[20];
    Dog *next;                         // Pointer to next Dog structure
};

int main(void)
{
    Dog *first = NULL;                 // Pointer to first dog
    Dog *current = NULL;               // Pointer to current dog
    Dog *previous = NULL;              // Pointer to previous dog

    char test = '\0';                    // Test value for ending input

    for (; ; )
    {
        printf_s("Do you want to enter details of a%s dog (Y or N)? ",
            first != NULL ? "nother" : "");
        scanf_s(" %c", &test, sizeof(test));
        if (tolower(test) == 'n')
            break;

        //  为Dog结构分配内存
        current = (Dog*)malloc(sizeof(Dog));
        if (first == NULL)                  // If there's no 1st Dog...
            first = current;                 // ...set this as 1st Dog

        if (previous != NULL)               // If there was a previous...
            previous->next = current;        // ...set its next to this one

        printf_s("Enter the name of the dog: ");
        scanf_s("%s", current->name, sizeof(current->name));

        printf_s("How old is %s? ", current->name);
        scanf_s("%d", ¤t->age);

        printf_s("How high is %s ( in hands )? ", current->name);
        scanf_s("%d", ¤t->height);

        printf_s("Who is %s's father? ", current->name);
        scanf_s("%s", current->father, sizeof(current->father));

        printf_s("Who is %s's mother? ", current->name);
        scanf_s("%s", current->mother, sizeof(current->mother));

        current->next = NULL;             // In case it's the last...
        previous = current;               // ...save its address
    }

    // 打印结果
    printf_s("\n");
    current = first;                    // Start at the beginning
    while (current != NULL)             // As long as we have a valid pointer
    { // Output the data
        printf_s("%s is %d years old, %d hands high,",
            current->name, current->age, current->height);
        printf_s(" and has %s and %s as parents.\n", current->father,
            current->mother);
        previous = current;               // Save the pointer so we can free memory
        current = current->next;          // Get the pointer to the next
        free(previous);                   // Free memory for the old one
        previous = NULL;
    }
    first = NULL;
    system("pause");
    return 0;
}

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

Do you want to enter details of a dog (Y or N)? y
Enter the name of the dog: goodboy
How old is goodboy? 2
How high is goodboy ( in hands )? 2
Who is goodboy's father? Fgoodboy
Who is goodboy's mother? Mgoodboy
Do you want to enter details of another dog (Y or N)? n

goodboy is 2 years old, 2 hands high, and has Fgoodboy and Mgoodboy as parents.