1、结构体的定义

struct 结构体类型名

类型名 成员名;
类型名 成员名;
……
};

先声明结构体类型,再定义结构体变量名

声明结构体类型,不分配空间

定义结构体类型变量,就要分配内存空间

1)结构体被分配的内存空间

struct student
{
    int num;//4字节
    char name[20];//20字节
    char sex;//1字节,但因为内存对齐占4字节
    int age;//4字节
    double score;//8字节
    char addr[30];//30字节,内存对齐占32字节
};
int main()
{
    struct student stu1,stu2;
    printf("%d\n",sizeof(stu1));
    return 0;
}

2)结构体的嵌套

/*
*copyright(c) 2018,HH
*All rights reserved.
*作 者:HH
*完成日期:2018年8月15日
*版本号:v1.0
*
*问题描述:结构体嵌套、赋值、输出
*输入描述:;
*程序输出:
*/

/*
*copyright(c) 2018,HH
*All rights reserved.
*作 者:HH
*完成日期:2018年8月15日
*版本号:v1.0
*
*问题描述:结构体嵌套、赋值、输出
*输入描述:;
*程序输出:
*/
 
#include<stdio.h>
#include<string.h>
struct date
{
    int month;
    int day;
    int year;
};
typedef struct 
{
    int num;
    char name[20];
    char sex;
    struct date birthday;
    float score;
}strcut;
 strcut  student1;
 strcut  student2={10002,"MaYun",'f',8,28,2000,99.9999};
int main()
{
    student1=student2;
    printf("%d\n",student1.num);
    printf("%s\n",student1.name);
    printf("%c\n",student1.sex);
    printf("%d\n",student1.birthday.month);
    printf("%d\n",student1.birthday.day);
    printf("%d\n",student1.birthday.year);
    printf("%f\n",student1.score);
    return 0;
}

3)结构体的成员是数组

/*
*copyright(c) 2018,HH
*All rights reserved.
*作 者:HH
*完成日期:2018年8月14日
*版本号:v1.0
*
*问题描述:结构体输出
*输入描述:;
*程序输出:
*/

#include<stdio.h>
#include<string.h>
struct student//自定义结构体
{
    int num;//学号
    char name[10];//名字
    double score[3];//三科成绩
};
void print(struct student s)//自定义输出函数
{
    printf("%ld %s\n",s.num,s.name);
    printf("%1f %1f %1f\n",s.score[0],s.score[1],s.score[2]);
}

int main()
{
    struct student stu;
    stu.num=42001;
    strcpy(stu.name,"MaHuTeng");//字符串赋值函数,将字符串复制到数据项name中
    stu.score[0]=98,stu.score[1]=99,stu.score[2]=100;
    print(stu);
    return 0;
}

2、结构体数组

定义:该数组的每个元素都是一个结构体。

结构体数组的初始化及直接输出方法:

/*
*copyright(c) 2018,HH
*All rights reserved.
*作 者:HH
*完成日期:2018年8月16日
*版本号:v1.0
*
*问题描述:结构体数组的正确初始化及输出
*输入描述:;
*程序输出:
*/

#include<stdio.h>
#include<string.h>
struct student
{
    int num;
    char name[20];
    char sex;
    int age;
    float score;
    char addr[30];
};
int main()
{
    int i;
    struct student stu[3]={{10101,"LiuHu",'F',18,99.7,"beijing road"},{10102,"NaoDan",'M',17,99.8,"shanghai road"},{10103,"MaHuangTeng",'F',48,99.9,"shenzhen road"}};
    for(i=0;i<3;i++)
    {
       printf("%d %s %c %d %1f %s\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].age,stu[i].score,stu[i].addr);
    }
    return 0;
}

以下为自定义函数调用的方式实现结构体数组的输出:

/*
*copyright(c) 2018,HH
*All rights reserved.
*作 者:HH
*完成日期:2018年8月16日
*版本号:v1.0
*
*问题描述:结构体数组的正确初始化及输出(函数调用的方法)
*输入描述:;
*程序输出:
*注意,数组地址传递容易出错
*/

#include<stdio.h>
#include<string.h>
struct student
{
    int num;
    char name[20];
    char sex;
    int age;
    float score;
    char addr[30];
};
struct student s[3];
void print(struct student s[3])//自定义输出函数以供调用
{
    int i;
    for(i=0;i<3;i++)
    {
        printf("%d %s %c\n",s[i].num,s[i].name,s[i].sex);
        printf("%d %1f %s\n",s[i].age,s[i].score,s[i].addr);
    }
}
int main()
{
    struct student stu[3]={{10101,"LiuHu",'F',18,99.7,"beijing road"},{10102,"NaoDan",'M',17,99.8,"shanghai road"},{10103,"MaHuangTeng",'F',48,99.9,"shenzhen road"}};
    print(stu);//若此处写成print(stu[])则会报错expected expression before'print'
    return 0;
}

在此方法中,由于print()函数的定义

print(stu)不会报错

而print (stu[ ])会报错,

数组名等价于等同于指向数组首元素的指针,stu[ ]代表的是数组,而stu代表指向的数组首地址的指针

报错原因是,因为被调用函数的形参对象是数组名,而不是数组!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐