学习来源 b站--作者:小甲鱼--【C语言】C语言视频教程(孤独自学 - up主)
结构体
一般格式:
struct 结构名
{
成员表列
};
成员的一般形式:类型说明符 成员名;
例如:
struct student
{
int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
}student
引用结构体变量中的成员的方式:结构体变量名.成员名。“.”是成员(分量)运算符,它在所有的运算符中优先级最高。
注:
(1)不能将一个结构体变量作为一个整体进行输入和输出。
(2) 对结构体变量的成员可以像普通变量一样进行各种运算(根据其类型决定可以进行的运算)。
(3)如果成员本身又属一个结构体类型,则要用若干个成员运算符,一级一级地找到最低的一级的成员。只能对最低级的成员进行赋值或存取以及运算。
(4)可以引用结构体变量成员的地址,也可以引用结构体变量的地址。(结构体变量的地址主要用作函数参数,传递结构体变量的地址。)
结构体例子
#include <stdio.h>
void main()
{
struct date
{
int month;
int day;
int year;
};
struct
{
int num;
char name[20];
char sex;
struct date birthday;
float score;
} boy1, boy2;
scanf("%d", &boy1.birthday.year);
scanf("%d", &boy1.birthday.month);
scanf("%d", &boy1.birthday.day);
printf("\n");
boy2 = boy1;
printf("boy1's birthday is %d-%d-%d\n", boy1.birthday.year, boy1.birthday.month, boy1.birthday.day);
printf("boy2's birthday is %d-%d-%d\n", boy2.birthday.year, boy2.birthday.month, boy2.birthday.day);
}
输入:
2000
01
01
输出:
boy1's birthday is 2000-1-1
boy2's birthday is 2000-1-1
结构体的几种初始化的方式
#include <stdio.h>
void main()
{
struct student /*定义结构*/
{
int num;
char *name;
char sex;
float score;
}boy1 = { 102, "Jane", 'M', 98.5 };
struct student boy2 = {55,"cat","F",82.2};
struct student boys[] = { {1,"Jack", "M", 60.5}, {5,"Lily","F",90.8} };
printf("Number = %d Name = %s Score = %.1f", boy1.num, boy1.name, boy1.score);
printf("\n");
printf("Number = %d Name = %s Score = %.1f", boy2.num, boy2.name, boy2.score);
printf("\n");
printf("Number = %d Name = %s Score = %.1f", boys[0].num, boys[0].name, boys[0].score);
printf("\n");
printf("Number = %d Name = %s Score = %.1f", boys[1].num, boys[1].name, boys[1].score);
}
输出:
Number = 102 Name = Jane Score = 98.5
Number = 55 Name = cat Score = 82.2
Number = 1 Name = Jack Score = 60.5
Number = 5 Name = Lily Score = 90.8
指向结构体类型数据的指针
一个结构体变量的指针就是该结构体变量所占据的内存段的起始地址。可以设一个指针变量,用来指向一个结构体变量,此时该指针变量的值是结构体变量的起始地址。指针变量也可以用来指向结构体数组中的元素。其中赋值时是把结构变量的首地址赋予该指针变量,不能把结构名赋予该指针变量。
一般形式:struct 结构名 *结构指针变量名
//赋值操作
stu = &bos //bos是student的结构变量,这是正确的
//stu = &student 这是错的
其访问成员的方式是:(*结构指针变量).成员名 或者是:结构指针变量
访问方式的例子
#include <stdio.h>
struct stu
{
int num;
char *name;
char sex;
float score;
} boy1 = {102, "Fishc", 'M', 78.5};
void main()
{
struct stu *pstu;
pstu = &boy1;
printf("Number = %d\nName = %s\n", boy1.num, boy1.name);
printf("Sex = %c\nScore = %f\n\n", boy1.sex, boy1.score);
printf("Number = %d\nName = %s\n", (*pstu).num, (*pstu).name);
printf("Sex = %c\nScore = %f\n\n", (*pstu).sex, (*pstu).score);
printf("Number = %d\nName = %s\n", pstu->num, pstu->name);
printf("Sex = %c\nScore = %f\n\n", pstu->sex, pstu->score);
}
输出:
Number = 102
Name = Fishc
Sex = M
Score = 78.500000Number = 102
Name = Fishc
Sex = M
Score = 78.500000Number = 102
Name = Fishc
Sex = M
Score = 78.500000
题目:有一个结构体变量stu,内含学生学号、姓名和3门课程的成绩。通过调用函数print中将它们输出。第一次使用结构体变量作为参数,第二次使用指向结构体变量的指针作为实参。
解法1:
#include <stdio.h>
#include <string.h>
struct student
{
int num;
char *name;
float score[3];
};
void print(struct student);
void main()
{
struct student stu;
stu.num = 8;
stu.name = "Jack";
stu.score[0] = 98.5;
stu.score[1] = 99.0;
stu.score[2] = 99.5;
print( stu );
}
void print( struct student stu )
{
printf("\tnum : %d\n", stu.num);
printf("\tname : %s\n", stu.name);
printf("\tscore_1 : %5.2f\n", stu.score[0]);
printf("\tscore_2 : %5.2f\n", stu.score[1]);
printf("\tscore_3 : %5.2f\n", stu.score[2]);
printf("\n");
}
结果:
num : 8
name : Jack
score_1 : 98.50
score_2 : 99.00
score_3 : 99.50
解法2:
#include <stdio.h>
#include <string.h>
struct student
{
int num;
char name[20];
float score[3];
};
void print(struct student *);
void main()
{
struct student stu;
stu.num = 8;
strcpy(stu.name, "Jack");
stu.score[0] = 98.5;
stu.score[1] = 99.0;
stu.score[2] = 99.5;
print( &stu );
}
void print( struct student *p )
{
printf("\tnum : %d\n", p -> num);
printf("\tname : %s\n", p -> name);
printf("\tscore_1 : %5.2f\n", p -> score[0]);
printf("\tscore_2 : %5.2f\n", p -> score[1]);
printf("\tscore_3 : %5.2f\n", p -> score[2]);
printf("\n");
}
结果:
num : 8
name : Jack
score_1 : 98.50
score_2 : 99.00
score_3 : 99.50
今日先学到这,在结构体中还有很多内容,继续加油!!