C++学习笔记day5

指针

1、指针的定义与使用

#include<iostream>
using namespace std;



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;





	system("pause");

	return 0;
}

2、指针所占用内存空间

#include<iostream>
using namespace std;


int main()
{
	//指针所占内存空间
	int a = 10;
	int *p = &a;
	//在32位操作系统下(x86),不管什么数据类型,指针都是只占4个字节。
	//在64位操作系统下(x64),不管什么数据类型,指针都是只占8个字节。
	cout << "sizeof(int *)= " << sizeof(int *) << endl;
	cout << "sizeof(float *)= " << sizeof(float *) << endl;
	cout << "sizeof(double *)= " << sizeof(double *) << endl;
	system("pause");


	return 0;
}

3、空指针和野指针

#include<iostream>
using namespace std;


int main()
{
	//空指针和野指针都不是我们自己申请的空间,因此不要访问。

	//空指针
	//1、空指针用于给指针变量进行初始化
	int *p = NULL;
	//2、空指针是不可以访问的

	//0~255之间的内存编号是系统占用的,因此不可以访问

	//野指针:指针变量指向非法的内存空间

	//在程序中,尽量避免出现野指针
	int *p =(int *) 0x1100;
	system("pause");


	return 0;

}

4、const修饰指针

#include<iostream>
using namespace std;


int main()
{
	//1、const修饰指针--常量指针
	//指针的指向可以修改,但是指针指向的值不可以改
	int a = 10;
	int b = 20;
	const int *p = &a;
	//*p = 20;错误

	p = &b;//正确

	//2、const修饰常量--指针常量
	//指针的指向不可以修改,但是指针指向的值可以改
	int *const p2 = &a;
	*p2 = 100;//正确

	//p2 = &b;//错误

	//3、const既修饰指针,又修饰常量
	//指针的指向和指针指向的值都不可以改
	const int *const p3 = &a;
	//*p3 = 100;//错误

	//p3 = &b;//错误
	system("pause");

	return 0;

}

5、指针和数组

#include<iostream>
using namespace std;

int main()
{
	//指针和数组
	//利用指针访问数组中的元素

	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	cout << "按照数组访问元素:" << endl;
	for (int i = 0; i < 10; i++)
		cout << arr[i]<<" ";
	cout << endl;

	int *p = arr;//arr就是数组首地址
	int *p2 = arr;
	cout << "利用指针访问第一个元素:" << *p << endl;
	p++;//让指针向后偏移4个字节
	cout << "利用指针访问第二个元素:" << *p << endl;
	cout << "按照指针访问元素:";
	for (int i = 0; i < 10; i++)
		
		cout<< *(p2 + i) << " ";
	cout << endl;
	system("pause");


	return 0;

}

6、指针和函数

#include<iostream>
using namespace std;
//实现两个数字进行交换
void swap01(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
	cout << "swap01a=" << a << endl;
	cout << "swap01b=" << b << endl;
}
void swap02(int *p1, int *p2)
{
	int temp = *p1;
	*p1 = *p2;
	*p2= temp;
	cout << "swap02*p1=" << *p1 << endl;
	cout << "swap02*p2=" << *p2 << endl;
}
int main()
{
	//指针和函数
	//1、值传递

	int a = 10;
	int b = 20;
	swap01(a, b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	//2.按照地址传递
	//如果是地址传递,可以修饰实参

	swap02(&a, &b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	system("pause");


	return 0;

}

7、指针,数组,函数

#include<iostream>
using namespace std;
//冒泡排序
void bubblesort(int *arr,int len)

{

	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
		//如果j>j+1的值就交换数字
			if (arr[j] > arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}
void printArray(int *arr, int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << arr[i] << endl;

	}
}
int main()
{
	//1、先创建数组
	int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };

	//数组长度
	int len = sizeof(arr) / sizeof(arr[0]);


	//2、创建函数,实现冒泡排序

	bubblesort(arr, len);
	//3、打印排序后的数组
	printArray(arr, len);
	system("pause");


	return 0;


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值