#include <iostream>
#include<string>
using namespace std;
struct Student
{
string name;
int age;
int score;
}s2;
struct teacher {
int id;
string name;
struct Student stu;
};
int main()
{
//1. 定义指针
int a = 10;
//指针定义的语法 : 数据类型 * 指针变量
int* p;
//让指针记录变量a的地址
p = &a;
cout << "a的地址为:" << &a << endl;
cout << "p的地址为:" << p << endl;
//2. 使用指针
//可以通过解引用的方式来找到指针指向的内存
//指针前加 * 代表解引用,找到指针指向的内存中的数据
*p = 1000;
cout << "a=" << a << endl;
cout << "*p=" << *p << endl;
//3 指针所占内存空间
cout<< "int*:" << sizeof(int*) << endl;
cout <<"float*:" << sizeof(float*) << endl;
cout <<"double*:" << sizeof(double*) << endl;
cout <<"char*:" << sizeof(char*) << endl;
//4 空指针和野指针
//注意: 空指针指向的内存是不可以访问的
int* p2 = NULL;
//cout << "*p:" << *p2 << endl;
//空指针 1~255 系统自带
//野指针
//int* p3 = (int*)0x11000;
//cout << "*p3:" << *p3 << endl;
//5 const 修饰指针 常量指针
cout << endl;
int a1 = 10;
int b1 = 20;
int* p4 = &a1;
cout << "a1: " << &a1 << endl;
cout << "b1: " << &b1 << endl;
const int* p5 = &a1;
//常量指针
//特点: 指针的指向可以修改,
//但是指针指向的值不可以改
//*p5 = 55; 错误
cout << "a1 p5: " << p5 << endl;
p5 = &b1;
cout << *p5 << endl;
cout << "b1 p5: " << p5 << endl;
//指针常量
//特点: 指针的指向不可以改,
//指针指向的值可以改
int* const p6 = &b1;
b1 = 30;
//p6 = &a1; 错误
cout << *p6 << endl;
return 0;
}
//6 指针和数组
//利用指针访问数组中的元素
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
cout << "第一个元素为:" << arr1[0] << endl;
int* p7 = arr1;
cout << "sizeof(arr1): " << sizeof(arr1) << endl;
for (int i = 0; i < sizeof(arr1)/sizeof(arr1[0]); i++)
{
cout <<":"<< *(p7 + i) << endl;
}
//7 指针和函数
//作用:利用指针作函数参数,可以修改实参的值
//1 值传递 不会改变实参
//2 地址传递
#include <iostream>
using namespace std;
void swap1(int *p1,int *p2)
{
int temp=*p1;
*p1=*p2;
*p2=temp;
}
int main()
{
int a=10;
int b=20;
swap1(&a,&b);
cout<<"a->"<<a<<endl;
cout<<"b->"<<b<<endl;
return 0;
}
//8 指针 数组 函数
//9 结构体
//自定义数据类型, 一些类型集合组成的一个类型
//语法 struct 类型名称 {成员列表}
Student s1;
s1.name = "你好,世界!!!";
s1.age = 18;
s1.score = 100;
cout << "name: " << s1.name << " age: " << s1.age << " score: " << s1.score << endl;
s2.name = "tiny";
s2.age = 18;
s2.score = 100;
cout << "s2->name: " << s2.name << " age: " << s2.age << " score: " << s2.score << endl;
//定义结构体时的关键字是 struct , 不可以省略;
//创建结构体变量时,关键字struct可以省略;
//结构体变量利用操作符 "." 访问成员
//结构体数组
//1 定义结构体
//2 创建结构体数组
Student s3[5] = {
{"张塞",19,110},
{"里斯",66,89},
{"李一",45,77}
};
//3 给结构体数组中的元素赋值
s3[2].name = "李炳";
s3[2].age = 19;
s3[2].score = 99;
//4 遍历结构体数组
for (int i = 0; i < 5; i++)
{
cout << "s3->name: " << s3[i].name << " age: " << s3[i].age << " score: " << s3[i].score << endl;
}
//10 结构体指针
//1 创建学生结构体变量
struct Student s4 = {"张三",31,87};
//2 通过指针指向结构体变量
Student* p10 = &s4;
//3 通过指针访问结构体变量中的数据
cout << "s4->name: " << p10->name << " age: " << p10->age << " score: " << p10->score << endl;
struct teacher t;
t.id = 10000;
t.name = "laowa";
t.stu.name = "小王子";
t.stu.age = 12;
t.stu.score = 78;
cout << "--" << t.id << "--" << t.name << "--" << t.stu.name << "--" << t.stu.age << "--" << t.stu.score << endl;
//11 结构体做函数参数
//
//总结: 不想修改主函数中的数据,用值传递,反之用地址传递
struct Student{
#include <iostream>
using namespace std;
struct Student{
string name;
int age;
int score;
};
void Print1(struct Student s5)
{
s5.age=666;
cout << "值传递--" << s5.name << "--" << s5.age << "--" <<s5.score << endl;
}
void Print2(struct Student*p)
{
//p->age=100;
cout << "地址传递--" << p->name << "--" << p->age << "--" <<p->score << endl;
}
int main()
{
//11 结构体做函数参数
//
//创建结构体变量
Student s5;
s5.name = "小库";
s5.age = 29;
s5.score = 86;
//1 值传递
Print1(s5);
//2 地址传递
Print2(&s5);
cout << "值传递--" << s5.name << "--" << s5.age << "--" <<s5.score << endl;
return 0;
}
//11 结构体中const 使用场景
//请参考 5
请大佬 点赞