结构体的定义和使用
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型!
1)定义一个结构体
//1.struct 结构体名 变量名
struct student stu1;//struct 关键字可省略
//2.struct 结构体名 变量名={成员1值,成员2值...}
struct student stu2={"李四",19,60};
//3.定义结构体时顺便创建变量
struct student
{
string name;
int age;
int score;
}stu3;
结构体案例:
声明一个表示时间的结构体
#include<iostream>
using namespace std;
struct maytime
{
int year;
int month;
int day;
int hour;
int sec;
}s5;
int main()
{
struct maytime zyy;
cout<<"enter year"<<endl;
cin>>zyy.year;
cout<<"enter month"<<endl;
cin>>zyy.month;
cout<<"enter day"<<endl;
cin>>zyy.day;
cout<<"enter hour"<<endl;
cin>>zyy.hour;
cout<<"enter second"<<endl;
cin>>zyy.sec;
cout<<"maytime:"<<zyy.year<<"/"<<zyy.month<<"/"<<zyy.day<<" "<<zyy.hour<<":"<<zyy.sec<<endl;
return 0;
}
2)结构体数组
定义结构体 struct 结构体名 数组名[元素个数]={{},{},…{}}
//构建结构体数组
struct student stuArray[2]=
{
{"张三",18,100},
{"李四”,19,80}
}
//给结构体数组中的元素赋值
stuArray[1].name="赵六';
stuArray[1].age=10;
//遍历结构体数组
for(int i=0;i<2;i++)
{
cout<<"姓名:"<<stuArray[i].name;
}
3)结构体指针
通过指针指向结构体变量
struct student *p=&s;
通过指针访问结构体变量中的数据
p->name;p->age;
(通过结构体指针访问结构体变量需要利用’->’)
4)结构体嵌套结构体
t.stu.score;
5) 结构体做函数参数
将函数体形参改为指针,可以减少内存空间,而且不会复制新的副本出来。
//学生结构体定义
struct student
{
//成员列表
string name; //姓名
int age; //年龄
int score; //分数
};
//值传递
void printStudent(student stu )
{
stu.age = 28;
cout << "子函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
}
//地址传递
void printStudent2(student *stu)
{
stu->age = 28;
cout << "子函数中 姓名:" << stu->name << " 年龄: " << stu->age << " 分数:" << stu->score << endl;
}
int main() {
student stu = { "张三",18,100};
//值传递
printStudent(stu);
cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
cout << endl;
//地址传递
printStudent2(&stu);
cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
system("pause");
return 0;
}
6) 结构体中const的使用场景
void printstudents(const student *s)
加入const之后,一旦有修改的操作就会报错,可以防止我们误操作。
有符号整数和无符号整数
无符号整数只能输出大于0的数
有符号整数和无符号整数,在计算机内部如何区分?
- 有符号整数在计算机内以二进制补码形式储存,其最高位表示符号,0为正,1为负;无符号整数只能为正数。
break和continue的区别
- break是结束整个循环体,continue是结束单次循环。
enum枚举类型应用
枚举类型的定义:
//(1)定义了一个枚举类型名 enum weekday,然后定义变量为该枚举类型。例如:
enum weekday{sun,mon,tue,wed,thu,fri,sat};
enum weekday day;
//(2)直接定义枚举类型变量。例如:
enum weekday{sun,mon,tue,wed,thu,fri,sat} day;
// (3)用typedef关键字将枚举类型定义成别名,并利用该别名进行变量声明:
typedef enum workday
{
saturday,
sunday = 0,
monday,
tuesday,
wednesday,
thursday,
friday
} workday; //此处的workday为枚举型enum workday的别名
workday today, tomorrow; //变量today和tomorrow的类型为枚举型workday,也即enum workday
notes:
-
枚举元素不是变量,而是常数,因此枚举元素又称为枚举常量。因为是常量,所以不能对枚举元素进行赋值。
-
一个整数不能直接赋给一个枚举变量,必须强制进行类型转换才能赋值。
例如: day=(enum weekday)2。
-
C++声明完枚举类型后,声明变量时,可以省略enum。
-
枚举元素具有默认值
goto语句的使用
跳转
goto Flag;
xxxx
xxxx
Flag:
xxxx
const修饰指针
1.const int *p=&a;
常量指针——const修饰指针
特点:指针的指向可以修改,但是指针指向的值不可以改。
eg:*p=20;错误,指针指向的值不可以改
p=&b;正确,指针指向可以改
2.int *const p=&a——const修饰常量
指针常量
特点:指针的指向不可以改,指针指向的值可以改。
*p=20;正确,指针指向的值可以改
p=&b;错误,指针指向不可以改
3.const int *const p=&a;
const既修饰指针,又是又修饰常量
特点:指针的指向和指针指向的值都不可以修改
指针和数组
int arr[10]={1,2,3}
int *p=arr;//arr为数组首地址
p++;//让地址往后偏移4个字节
//利用指针遍历数组
for(int i=0;i<10;i++)
{
cout<<*p<<endl;
p++;
}
typedef声明
- 用于将一个标识符声明成某个数据类型的别名,然后将该标识符当作数据类型使用
语法:
typedef 已有类型名 新类型名表;
变量的存储类型
- auto存储类型:采用堆栈的方式分配内存空间,属于一时性存储,其存储空间可以被若干变量多次覆盖使用。
- register存储类型:存放在通用寄存器中。
- extern存储类型:在所有函数和程序段中都可以引用。
- static存储类型:在内存中是以固定地址存放的,在整个程序运行期间都有效。