这里是指针 ,结构体部分
1指针
1.1指针
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int *p;
p = &a;
cout << "a的地址为:" << &a << endl;
cout << p << endl;
cout << *p << endl;
cout << &p << endl;
*p = 1000;
cout << "修改后" << endl;
cout << a << endl;
cout << *p << endl;
cout << sizeof(int *) << endl;
cout << sizeof(p) << endl;
cout << sizeof(*p) << endl;
int *p = NULL;
*p = 1000;
int *p = (int*)0x1100;
cout << *p << endl;
int a = 10;
int b = 20;
const int *p = &a;
int *const p = &a;
const int *const p = &a;
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int *p;
p = arr;
cout << p << endl;
cout << arr[0] << endl;
cout << "循环打印" << endl;
for (int i = 0; i < 9; i++)
{
cout << *p << endl;
p++;
}
system("pause");
return 0;
}
1.2指针和函数
void swap01(int num1, int num2)
{
int flag = num1;
num1 = num2;
num2 = flag;
cout << num1 << endl;
cout << num2 << endl;
}
void swap02(int *p1, int *p2)
{
int flag = *p1;
*p1 = *p2;
*p2 = flag;
cout << "指针" << endl;
cout << *p1 << endl;
cout << *p2 << endl;
}
int main()
{
int a = 10;
int b = 20;
swap02(&a, &b);
cout << a << endl;
cout << b << endl;
system("pause");
return 0;
}
冒泡升序
void sort(int *p,int *p2);
int main()
{
int arr[10] = { 4, 3, 6, 9, 1, 2, 10, 8, 7, 5 };
int num = sizeof(arr) / sizeof(arr[0]);
sort(arr,&num);
for (int k = 0; k < num; k++)
{
cout << arr[k] << endl;
}
system("pause");
return 0;
}
void sort(int *p,int *p2)
{
for (int i = 0; i <10; i++)
{
for (int j = 0; j < *p2 - i - 1; j++)
{
if (*(p + j)>*(p + j + 1))
{
int flag = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = flag;
}
}
}
}
2 结构体
2.1 结构体
#include<iostream>
using namespace std;
#include<string>
#include<ctime>
struct student
{
string name;
int age;
int score;
}s3;
int main()
{
struct student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout << "姓名:" << s1.name << "年龄:" << s1.age << "分数:" << s1.score << endl;
struct student s2 = { "李四", 19, 90 };
cout << "姓名:" << s2.name << "年龄:" << s2.age << "分数:" << s2.score << endl;
s3.name = "王五";
s3.age = 20;
s3.score = 80;
cout << "姓名:" << s3.name << "年龄:" << s3.age << "分数:" << s3.score << endl;
struct student arr[2] =
{
{"嫖客",23,100},
{"胖子",22,100}
};
for (int i = 0; i < 2; i++)
{
cout << "姓名:" << arr[i].name << "年龄:" << arr[i].age << "分数:" << arr[i].score << endl;
}
system("pause");
return 0;
}
2.2 结构体指针
定义学生结构体
struct student
{
string name;
int age;
int score;
};
int main()
{
struct student s = { "李四", 19, 90 };
struct student *p = &s;
cout << p << endl;
cout << p->age << endl;
cout << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;
system("pause");
return 0;
}
2.3 结构体嵌套结构体
struct teacher
{
int id;
string name;
int age;
struct student stu;
};
struct student
{
string name;
int age;
int score;
};
int main()
{
struct teacher t;
t.id = 10000;
t.name = "老王";
t.age = 40;
t.stu.name = "胖子";
t.stu.age = 20;
t.stu.score = 60;
system("pause");
return 0;
}
2.4结构体作为函数参数
struct student
{
string name;
int age;
int score;
};
void print(struct student stu)
{
cout <<"值传递:"<< "姓名:" << stu.name << "年龄:" << stu.age << "分数:" << stu.score << endl;
}
void print1(struct student *p)
{
cout << "地址传递:" << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;
}
int main()
{
struct student stu1 = { "张三", 14, 80 };
struct student stu2;
stu2.name = "李四";
stu2.age = 15;
stu2.score = 90;
print(stu2);
print1(&stu2);
system("pause");
return 0;
}
2.5const使用场景
struct student
{
string name;
int age;
int score;
};
void print(const struct student *stu)
{
stu->age = 200;
cout << "地址传递:" << "姓名:" << stu->name << "年龄:" << stu->age << "分数:" << stu->score << endl;
}
int main()
{
struct student s = { "张三", 15, 70 };
print(&s);
cout<<"main中: " << "姓名:" << s.name << "年龄:" << s.age << "分数:" << s.score << endl;
system("pause");
return 0;
}
2.6结构体案例1
struct student
{
string name;
int score;
};
struct teacher
{
string name;
struct student sarray[5];
};
void allocatespace(struct teacher tarray[3],int len)
{
string nameseed = "ABCDE";
for (int i = 0; i < len; i++)
{
tarray[i].name = "teacher_";
tarray[i].name += nameseed[i];
for (int j = 0; j < 5; j++)
{
tarray[i].sarray[j].name = "student_";
tarray[i].sarray[j].name = nameseed[j];
tarray[i].sarray[j].score = rand() % 41 + 60;
}
}
}
void print(struct teacher tarray[],int len)
{
for (int i = 0; i < len; i++)
{
cout << "老师姓名:" << tarray[i].name << endl;
for (int j = 0; j < 5; j++)
{
cout << "\t学生姓名:" << tarray[i].sarray[j].name << "考试分数" << tarray[i].sarray[j].score << endl;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
struct teacher tarray[3];
int len = sizeof(tarray) / sizeof(tarray[0]);
allocatespace(tarray, len);
print(tarray, len);
system("pause");
return 0;
}
2.7 结构体案例2
struct hero
{
string name;
int age;
string sex;
};
void sort(struct hero harray[], int len)
{
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (harray[j].age > harray[j + 1].age)
{
struct hero array = harray[j];
harray[j] = harray[j + 1];
harray[j + 1] = array;
}
}
}
}
void print(struct hero harray[], int len)
{
cout << "排序后:" << endl;
for (int k = 0; k < len; k++)
{
cout << "姓名:" << harray[k].name << " 年龄:" << harray[k].age << " 性别:" << harray[k].sex << endl;
}
}
int main()
{
struct hero harray[5] =
{
{ "刘备", 23, "男" },
{ "关羽", 22, "男" },
{ "张飞", 20, "男" },
{ "赵云", 21, "男" },
{ "貂蝉", 19, "女" },
};
int len = sizeof(harray) / sizeof(harray[0]);
sort(harray, len);
print(harray,len);
system("pause");
return 0;
}