c语言结构体的声明和初步操作

    xiaoxiao2021-03-25  53

    一、结构体的声明

    #include <stdio.h>

    #include<stdlib.h>

    struct student {

    int num;

    char sex;

    int age;

    struct student *next;

    };

    int main() {

    int i = 0;

    struct student std1, std2;

    //std0={"",''0,std1};

    std1.num = 1;

    std1.sex = 'M';

    std1.age = 24;

    std1.next = &std2;

    std2.num = 2;

    std2.sex = 'w';

    std2.age = 25;

    std2.next = NULL;

    printf("%d\n", std1.next->age);

    system("pause");

    }

    二、结构体的操作

    #include <stdio.h>

    #include<stdlib.h>

    struct student {

    int num;

    char sex;

    int age;

    struct student *next;

    };

    int main() {

    int i = 0;

    struct student *head = NULL;

    struct student std1 = { 1,'m',19 };

    struct student std2 = { 2,'w',21 };

    struct student std3 = { 3,'m',23 };

    struct student std4 = { 5,'m',2 };

    head = &std1;

    std1.next = &std2;

    std2.next = &std3;

    std3.next = NULL;

    std1.next = &std4;

    std4.next = &std2;

    //查找年龄为a2的所在位置,及输出该位置的性别

    while (head != NULL) {

    i++;

    if (head->age == 2) {

    printf("%c,%d", head->sex, i);

    break;

    }

    else { head = head->next; }

    }

    system("pause");

    return 0;

    }

    转载请注明原文地址: https://ju.6miu.com/read-37025.html

    最新回复(0)