设为首页收藏本站 |天气与日历| 2025-07-01 星期二 17:12:00 (建党节) 乙巳(蛇)年 六月初七 酉时
     
切换到窄版

私人站点

 找回密码
 立即注册
搜索
查看: 196|回复: 0

struct 结构体变量的定义和初始化

[复制链接]

954

主题

954

帖子

3879

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3879
发表于 2021-12-7 12:52:57 | 显示全部楼层 |阅读模式
定义结构体变量的方式:
l 先声明结构体类型再定义变量名
l 在声明类型的同时定义变量
l 直接定义结构体类型变量(无类型名)
file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml21148\wps3.jpg
结构体类型和结构体变量关系:
l 结构体类型:指定了一个结构体类型,它相当于一个模型,但其中并无具体数据,系统对之也不分配实际内存单元。
l 结构体变量:系统根据结构体类型(内部成员状况)为之分配空间。
//结构体类型的定义
struct stu
{
        char name[50];
        int age;
};
//先定义类型,再定义变量(常用)
struct stu s1 = { "mike", 18 };
//定义类型同时定义变量
struct stu2
{
        char name[50];
        int age;
}s2 = { "lily", 22 };
struct
{
        char name[50];
        int age;
}s3 = { "yuri", 25 };
9.1.3 结构体成员的使用
#include<stdio.h>
#include<string.h>
//结构体类型的定义
struct stu
{
        char name[50];
        int age;
};
int main()
{
        struct stu s1;
        //如果是普通变量,通过点运算符操作结构体成员
        strcpy(s1.name, "abc");
        s1.age = 18;
        printf("s1.name = %s, s1.age = %d\n", s1.name, s1.age);
        //如果是指针变量,通过->操作结构体成员
        strcpy((&s1)->name, "test");
        (&s1)->age = 22;
        printf("(&s1)->name = %s, (&s1)->age = %d\n", (&s1)->name, (&s1)->age);
        return 0;
}
9.1.4 结构体数组
#include <stdio.h>
//统计学生成绩
struct stu
{
        int num;
        char name[20];
        char sex;
        float score;
};
int main()
{
        //定义一个含有5个元素的结构体数组并将其初始化
        struct stu boy[5] = {
                { 101, "Li ping", 'M', 45 },                       
                { 102, "Zhang ping", 'M', 62.5 },
                { 103, "He fang", 'F', 92.5 },
                { 104, "Cheng ling", 'F', 87 },
                { 105, "Wang ming", 'M', 58 }};
        int i = 0;
        int c = 0;
        float ave, s = 0;
        for (i = 0; i < 5; i++)
        {
                s += boy.score;        //计算总分
                if (boy.score < 60)
                {
                        c += 1;                //统计不及格人的分数
                }
        }
        printf("s=%f\n", s);//打印总分数
        ave = s / 5;                                        //计算平均分数
        printf("average=%f\ncount=%d\n\n", ave, c); //打印平均分与不及格人数
        for (i = 0; i < 5; i++)
        {
                printf(" name=%s,  score=%f\n", boy.name, boy.score);
          // printf(" name=%s,  score=%f\n", (boy+i)->name, (boy+i)->score);
        }
        return 0;
}
9.1.5 结构体套结构体
#include <stdio.h>
struct person
{
        char name[20];
        char sex;
};
struct stu
{
        int id;
        struct person info;
};
int main()
{
        struct stu s[2] = { 1, "lily", 'F', 2, "yuri", 'M' };
        int i = 0;
        for (i = 0; i < 2; i++)
        {
                printf("id = %d\tinfo.name=%s\tinfo.sex=%c\n", s.id, s.info.name, s.info.sex);
        }
        return 0;
}
9.1.6 结构体赋值
#include<stdio.h>
#include<string.h>
//结构体类型的定义
struct stu
{
        char name[50];
        int age;
};
int main()
{
        struct stu s1;
        //如果是普通变量,通过点运算符操作结构体成员
        strcpy(s1.name, "abc");
        s1.age = 18;
        printf("s1.name = %s, s1.age = %d\n", s1.name, s1.age);
        //相同类型的两个结构体变量,可以相互赋值
        //把s1成员变量的值拷贝给s2成员变量的内存
        //s1和s2只是成员变量的值一样而已,它们还是没有关系的两个变量
        struct stu s2 = s1;
//memcpy(&s2, &s1, sizeof(s1));
        printf("s2.name = %s, s2.age = %d\n", s2.name, s2.age);
        return 0;
}
9.1.7 结构体和指针
1)指向普通结构体变量的指针
#include<stdio.h>
//结构体类型的定义
struct stu
{
        char name[50];
        int age;
};
int main()
{
        struct stu s1 = { "lily", 18 };
        //如果是指针变量,通过->操作结构体成员
        struct stu *p = &s1;
        printf("p->name = %s, p->age=%d\n", p->name, p->age);
        printf("(*p).name = %s, (*p).age=%d\n",  (*p).name,  (*p).age);
        return 0;
}
2)堆区结构体变量
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
//结构体类型的定义
struct stu
{
        char name[50];
        int age;
};
int main()
{
        struct stu *p = NULL;
        p = (struct stu *)malloc(sizeof(struct  stu));
        //如果是指针变量,通过->操作结构体成员
        strcpy(p->name, "test");
        p->age = 22;
        printf("p->name = %s, p->age=%d\n", p->name, p->age);
        printf("(*p).name = %s, (*p).age=%d\n", (*p).name,  (*p).age);
        free(p);
        p = NULL;
        return 0;
}
3)结构体套一级指针
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
//结构体类型的定义
struct stu
{
        char *name; //一级指针
        int age;
};
int main()
{
        struct stu *p = NULL;
        p = (struct stu *)malloc(sizeof(struct  stu));
        p->name = malloc(strlen("test") + 1);
        strcpy(p->name, "test");
        p->age = 22;
        printf("p->name = %s, p->age=%d\n", p->name, p->age);
        printf("(*p).name = %s, (*p).age=%d\n", (*p).name, (*p).age);
        if (p->name != NULL)
        {
                free(p->name);
                p->name = NULL;
        }
        if (p != NULL)
        {
                free(p);
                p = NULL;
        }
        return 0;
}
9.1.8 结构体做函数参数
1)结构体普通变量做函数参数
#include<stdio.h>
#include <string.h>
//结构体类型的定义
struct stu
{
        char name[50];
        int age;
};
//函数参数为结构体普通变量
void set_stu(struct stu tmp)
{
        strcpy(tmp.name, "mike");
        tmp.age = 18;
        printf("tmp.name = %s, tmp.age = %d\n", tmp.name, tmp.age);
}
int main()
{
        struct stu s = { 0 };
        set_stu(s); //值传递
        printf("s.name = %s, s.age = %d\n", s.name, s.age);
        return 0;
}
2)结构体指针变量做函数参数
#include<stdio.h>
#include <string.h>
//结构体类型的定义
struct stu
{
        char name[50];
        int age;
};
//函数参数为结构体指针变量
void set_stu_pro(struct stu *tmp)
{
        strcpy(tmp->name, "mike");
        tmp->age = 18;
}
int main()
{
        struct stu s = { 0 };
        set_stu_pro(&s); //地址传递
        printf("s.name = %s, s.age = %d\n", s.name, s.age);
        return 0;
}
3)结构体数组名做函数参数
#include<stdio.h>
//结构体类型的定义
struct stu
{
        char name[50];
        int age;
};
//void set_stu_pro(struct stu tmp[100], int n)
//void set_stu_pro(struct stu tmp[], int n)
void set_stu_pro(struct stu *tmp, int n)
{
        int i = 0;
        for (i = 0; i < n; i++)
        {
                sprintf(tmp->name, "name%d%d%d", i, i, i);
                tmp->age = 20 + i;
                tmp++;
        }
}
int main()
{
        struct stu s[3] = { 0 };
        int i = 0;
        int n = sizeof(s) / sizeof(s[0]);
        set_stu_pro(s, n); //数组名传递
        for (i = 0; i < n; i++)
        {
                printf("%s, %d\n", s.name, s.age);
        }
        return 0;
}
4)const修饰结构体指针形参变量
//结构体类型的定义
struct stu
{
        char name[50];
        int age;
};
void fun1(struct stu * const p)
{
        //p = NULL; //err
        p->age = 10; //ok
}
//void fun2(struct stu const*  p)
void fun2(const struct stu *  p)
{
        p = NULL; //ok
        //p->age = 10; //err
}
void fun3(const struct stu * const p)
{
        //p = NULL; //err
        //p->age = 10; //err
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|编程站点 ( 冀ICP备2023028127号-2 )|友链申请|

GMT+8, 2025-7-1 17:12 , Processed in 0.096792 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表