指针
#include<iostream>
using namespace std;
void swap01(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
void swap02(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main56()
{
//指针
//通过指针间接访问内存
//内存编号从零开始记录,一般用十六位进制数表示
//可以用指针变量保存地址
int a = 10;
//指针: 数据类型 * 指针变量;
int * p;
p = &a;
cout << &a << endl;
cout << p << endl;
cout << *p << endl;
//指针前面加*代表解引用,找到指针指向内存中的数据
*p = 1000;
cout << a << endl;
cout << *p << endl;
cout << p << endl;
//指针所占的内存
//在32位操作系统下占用4个字节空间,64位操作系统下占用8个字节空间
int A = 10;
//int * P;
//P = &A;
int *P = &A;
//64位操作系统下占用8个字节空间
cout << sizeof(int *) << endl;
cout << sizeof(float *) << endl;
cout << sizeof(double *) << endl;
cout << sizeof(char *) << endl;
cout << sizeof(P) << endl;
//空指针和野指针
//空指针是指针变量指向内存中编号为零的空间
//空指针指向的内存是不可访问的
int* i = NULL;
//0-255之间的内存编号是系统占用的,因此不可访问
// *i = 100; error
//野指针
//指针变量指向非法的内存空间
int* j = (int*)0x1100;
//cout << *j << endl; error
//空指针,野指针都不是我们申请的空间,因此不要随意的访问它
//const修饰指针
//const修饰指针--常量指针(指针的指向可以改,指针指向的值不能改)const int * p=&a;
//const修饰常量--指针常量(指针的指向不可以改,指针指向的值可以改) int * const p=&a;
//const既修饰指针又修饰常量(指针的指向不可以改,指针指向的值不可以改)const int * const p=&a;
//const修饰指针--常量指针
int num1 = 10;
int num2 = 10;
const int* p1 = &a;
//*p=20;error
p1 = &num2;
//const修饰常量--指针常量
int * const p2 = &num1;
*p2 = 100;
//p2=&num2;error
//const既修饰指针又修饰常量
const int* const p3 = &num1;
//*p2 = 100;error
//p2=&num2;error
//指针和数组
//利用指针访问数组中的元素
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int* p4 = arr;
for (int i = 0; i < 10; i++)
{
cout << *p4 << endl;
p4++;//指针向后偏移4个字节
}
//指针和函数
//值传递(形参改变不了实参)
int a1 = 10;
int b1 = 20;
swap01(a1, b1);
cout << a1<<b1 << endl;
//地址传递(形参可以改变实参)
swap02(&a1, &b1);
cout << a1 << b1 << endl;
system("pause");
return 0;
}
指针数组函数的案例
#include<iostream>
using namespace std;
void bubblesort(int* arr, int len)
{
for (int i = 1; i < len; i++)
{
for (int j = 0; j < len - i ; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printarry(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
cout << arr[i] << endl;
}
}
int main63()
{
//封装一个函数,利用冒泡排序,实现对整型数组的升序排列
int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
int len = sizeof(arr) / sizeof(arr[0]);
bubblesort(arr, len);
printarry(arr, len);
system("pause");
return 0;
}
结构体
#include<iostream>
using namespace std;
#include<cstring>
int main64()
{
//结构体
//结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
//struct 结构体名{ 结构体成员列表 };(一些类型的集合组成的一个类型)
struct student
{
string name;
int age;
int score;
}s3;//顺便创建一个结构体变量
//创建具体的结构体
//1. struct student s1;
//2. struct student s2 = { ... };
//3. 在定义结构体时顺便创建结构体变量
struct student s1;//struct可以省略
//student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout << s1.name << s1.age << s1.score << endl;
struct student s2 = { "李四",19,80 };//这个struct也可以省略
cout << s2.name << s2.age << s2.score << endl;
s3.name = "王五";
s3.age = 20;
s3.score = 70;
cout << s3.name << s3.age << s3.score << endl;
system("pause");
return 0;
}
结构体数组
#include<iostream>
using namespace std;
//结构体数组,将自定义的结构体放到数组中方便维护
//struct 结构体名 数组名[元素个数]={{},{}....,{}}
struct student
{
string name;
int age;
int score;
};
int main()
{
struct student stuarray[3] =
{ {"张三", 18, 100},
{"李四", 19, 90},
{"王五", 20, 80},
};
stuarray[2].name = "赵六";
stuarray[2].age = 80;
stuarray[2].score = 20;
for (int i=0; i < 3; i++)
{
cout << stuarray[i].name << stuarray[i].age << stuarray[i].score << endl;
}
system("pause");
return 0;
}