第十二章、结构体和共用体

12.1结构体

数组:是将同种属性的数据进行一次处理
结构体:将不同类型的数据成员组织到统一的名字下
共用体:将不同类型的数据成员组织到统一的名字下,但是每一时刻只有一个数据成员起作用

struct student
{
long studentID;//学号
char studentName;//姓名
char studentSex;//性别
int yearOfBirth;//出生年
int score[4];//4门课的成绩
};
注意:结构体模板只声明了一种数据类型,并未声明结构体类型变量。编译器不为其分配内存空间,正如编译器不为int型分配内存一样

1、先声明结构体模板,在定义结构体变量
struct student stu1;
2、在声明结构体变量的同时定义结构体变量,也可以不出现结构体名(student)
struct student
{
long studentID;//学号
char studentName;//姓名
char studentSex;//性别
int yearOfBirth;//出生年
int score[4];//4门课的成绩
}stu1;

//用typedef定义数据类型
typedef int INTEGER;INTEGER和int是同义词。
typedef struct student STUDENT;
等价于
typedef struct student
{
long studentID;//学号
char studentName;//姓名
char studentSex;//性别
int yearOfBirth;//出生年
int score[4];//4门课的成绩
}STUDENT;
二者为struct student结构体定义了一个新名字STUDENT。

//结构体变量的初始化
STUDENT stu1={111,“张三”,“男”,1991,{80,80,80,80}};
等价于:
struct student stu1={111,“张三”,“男”,1991,{80,80,80,80}};

//嵌套的结构体
typedef struct date
{
int year;
int month;
int day;
}DATE;
typedef struct studet
{
long studentID;//学号
char studentName;//姓名
char studentSex;//性别
DATE Birthday;//出生年月日
int score[4];//4门课的成绩
}STUDENT;
STUDENT stu1={111,“张三”,“男”,{1998,6,4},{80,80,80,80}};

//结构体变量的引用和赋值
#include<stdio.h>
typedef struct date
{
 int year;
 int month;
 int day;
}DATE;
typedef struct student
{
 long studentID;
 char studentName[20];
 char studentSex;
 DATE birthday;
 int score[4];
}STUDENT;
int main()
{
 STUDENT stu1={111,"张三","男",{1998,6,4},{80,80,80,80}};
 STUDENT stu2;
 stu2=stu1;//相同类型的结构体的变量可以整体赋值
 printf("%ld%s%c%d/%d/%d%d%d%d%d\n",stu2.studentID,stu2.studentName,stu2.studentSex,stu2.birthday.year,stu2.birthday.month,stu2.birthday.day,stu2.score[0],stu2.score[1],stu2.score[2],stu2.score[3]);
 return 0;
}

//并非所有的结构体成员都可以用赋值运算符(=)进行赋值,对于字符数组类型的结构体成员赋值时,必须用字符串处理函数strcpy()。
//strcpy(stu2.studentName,stu2.studentName);
//在函数体外声明的结构体可以为所有函数使用,称为全局声明;在函数体内声明的结构体可以为所有函数使用,称为局部声明;
//结构体变量的地址是结构变量所占内存空间的首地址。而结构体成员的地址与结构体成员在结构体中的位置和该成员所占内存空间大小有关

//结构体所占内存的字节数
#include<stdio.h>
typedef struct sample
{
    char m1;
    int m2;
    char m3;
}SAMPLE;
int main()
{
    SAMPLE S={'A',2,'b'};
    printf("%d\n",sizeof(s));
    return 0;
}
输出:12;

//按道理应该1+1+4=6,但是为了满足处理器的对齐要求,可能会在较小的成员后加入补位。

//结构体数组的定义和初始化
#include<stdio.h>
typedef struct date
{
    int year;
    int month;
    int day;
}DATE;
typedef struct student
{
    long studentID;
    char studentName[20];
    char studentSex;
    DATE birthday;
    int score[4];
}STUDENT;
int main()
{
    int i,j,sum[30];
    STUDENT stu[30]={{111,"张三","男",{77,77,77,77}},{112,"李四","男",{77,77,77,77}},{113,"王五","女",{77,77,77,77}},{114,"周六","女",{77,77,77,77}}};
    for(i=0;i<4;i++)
    {
        sum[i]=0;
        for(j=0;j<4;j++)
        {
            sum[i]+=stu[i].score[j];
        }
        printf("%ld%s%c%d/%d/%d%d%d%d%d%f\n",stu2.studentID,stu2.studentName,stu2.studentSex,stu2.birthday.year,stu2.birthday.month,stu2.birthday.day,stu2.score[0],stu2.score[1],stu2.score[2],stu2.score[3],sum[i]/4.0);
    }
    return 0;
}

//结构体指针定义和初始化
/*
定义结构体指针并初始化:
STUDENT *p;
p=&stu1;
等价于
STUDENT *P=&stu1;
指针变量指向的结构体成员用指向运算符
p->studentID=111;
等价于:(*p).studentID=111;
p->birthday.year=1991;(对于结构体内部数组成员的访问)
*/

//向函数传递结构体
#include<stdio.h>
struct date
{
    int year;
    int month;
    int day;
};
//void fun(struct date *p)
void fun(struct date p)
{
    p.year=2000;
    p.month=5;
    p.day=22;
}
int main()
{
    struct date b;
    b.year=1999;
    b.month=4;
    b.day=21;
    //fun(&b);传址调用,结构体变量的成员值会修改
    fun(b);//结构体变量作为实参,传值调用,结构体变量的成员值不会修改
    return 0;
}


#include<stdio.h>
struct date
{
    int year;
    int month;
    int day;
};
struct date fun(struct date p)
{
    p.year=2000;
    p.month=5;
    p.day=22;
    return p;
}
int main()
{
    struct date b;
    b.year=1999;
    b.month=4;
    b.day=21;
    b=fun(b);//将函数返回值作为结构体变量的值
    return 0;
}
发布了47 篇原创文章 · 获赞 3 · 访问量 876

猜你喜欢

转载自blog.csdn.net/qq_42148307/article/details/105110578