当前位置: 首页 > news >正文

指针(个人学习笔记黑马学习)

1、指针的定义和使用

#include <iostream>
using namespace std;int main() {int a = 10;int* p;p = &a;cout << "a的地址为:" << &a << endl;cout << "a的地址为:" << p << endl;*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位操作系统下,指针是占4个字节空间大小,不管是什么数据类型//在64位操作系统下,指针是占8个字节空间大小,不管是什么数据类型cout << "指针占内存的大小:" << sizeof(int *) << endl;cout << "指针占内存的大小:" << sizeof(p) << endl;cout << "指针占内存的大小:" << sizeof(char *) << endl;cout << "指针占内存的大小:" << sizeof(float *) << endl;cout << "指针占内存的大小:" << sizeof(double *) << endl;system("pause");return 0;
}


3、空指针和野指针 

#include <iostream>
using namespace std;int main() {//空指针int* p = NULL;//野指针  尽量避免野指针int* p = (int*)0x1100;system("pause");return 0;
}

4、const修饰指针

#include <iostream>
using namespace std;int main() {//1、const修饰指针  常量指针 //指针指向的值不可以改,指针的指向可以改int a = 10;int b = 10;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 };int* p = arr;cout <<"利用指针访问第一个元素:"<< * p << endl;p++;cout << "利用指针访问第二个元素:" << *p << endl;int* p2 = arr;for (int i = 0; i < 10; i++){cout << *(p2 + i) << endl;}system("pause");return 0;
}


 6、指针和函数

#include <iostream>
using namespace std;void swap(int * p1 ,int * p2) {int temp = *p1;*p1 = *p2;*p2 = temp;}int main() {int a = 10;int b = 20;swap(&a, &b);cout << "a = " << a << endl;cout << "b = " << b << endl;system("pause");return 0;
}


7、案例 

案例描述:封装一个函数,利用冒泡排序,实现对整型数组的升序排序例如数组: int arr[10] ={ 4,3,6,9,1,2,10,8,7,5} ;

#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++) {if (arr[j] > arr[j + 1]) {int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}
}void printfArray(int* arr, int len) {for (int i = 0; i < len; i++) {cout << arr[i] << endl;}
}int main() {int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };int len = sizeof(arr) / sizeof(arr[0]);bubbleSort(arr,len);printfArray(arr, len);system("pause");return 0;
}

 

http://www.lryc.cn/news/145496.html

相关文章:

  • vue 路由动态加载
  • 电脑识别不了固态硬盘怎么办?
  • QCustomPlot 绘制卡顿问题
  • uni-app开发小程序,radio单选按钮,点击可以选中,再次点击可以取消
  • 【Qt专栏】实现单例程序,禁止程序多开的几种方式
  • 力扣26. 删除有序数组中的重复项
  • 【机器学习】鸢尾花分类-逻辑回归示例
  • Flink CDC介绍
  • Java集合sort排序报错UnsupportedOperationException处理
  • 安防监控/磁盘阵列存储/视频汇聚平台EasyCVR调用rtsp地址返回的IP不正确是什么原因?
  • Spring boot开启定时任务
  • package.json相关知识记录
  • VueRouter使用详解(5000字通关大全)
  • vue axios实现下载文件及responseType:blob注意事项
  • StringBuilder类分享(1)
  • Qt 打开文件列表选择文件,实现拖拽方式打开文件
  • [C/C++]天天酷跑游戏超详细教程-上篇
  • 5G NR:RACH流程 -- Msg1之选择正确的PRACH时频资源
  • 在vue3项目中编辑的时候,解决对话框里边的数据和列表中的数据联动了。深复制
  • 循环结构(个人学习笔记黑马学习)
  • ceph中PGLog处理流程
  • macOS使用命令行连接Oracle(SQL*Plus)
  • Mac下使用Homebrew安装MySQL5.7
  • centos安装Nginx配置Nginx
  • Linux环境下搭建使用缓存中间件Redis
  • Oracle 本地客户端连接远程 Oracle 服务端并使用 c# 连接测试
  • java中上传文件先下载到本地再上传还有就是直接通过文件流url地址进行上传优缺点?
  • 华为复合vlan(mux vlan)
  • 第62步 深度学习图像识别:多分类建模(Pytorch)
  • GPT带我学-设计模式-适配器模式