当前位置:网站首页>C语言 一级指针
C语言 一级指针
2022-07-29 05:09:00 【卷饼85】
一级指针
学习目标:
掌握一级指针知识
- 掌握一级指针知识
学习内容:
1. 指针基础 :
1.1指针的定义
指针定义: 数据类型 * 变量名
如:int * p
int 为指针的数据类型
p 为指针的变量名
对于 * 的作用就是引用指针指向的变量值,引用就是引用该变量的地址
int a = 10;
int* p = &a ;

& 在这里为取地址符
int * p = &a;为让p指针指向a的地址,如上图。
之后p的地址 0x002 中就存放的为a的地址 0x001。
1.2指针的大小
指针大小由CPU运行模式的寻址位数决定
一字节=8位
处理器在32位运行模式,逻辑寻址位数32,指针也就是32位,即4个字节
处理器在64位运行模式,逻辑寻址位数64,指针也就是64位,即8个字节
当在编译器运行程序时时,运行环境不同指针大小也不同
2. 指针解引用功能
解引用就是返回内存地址中对应的对象
解引用: * 指针类型的变量
如 1.1指针的定义 中代码 p 所在地址存储了 a 的地址,p 就等于 a 的地址,* p 就等于返回了 a 地址里的值。
修改 a 或 * p 的值就修改了 a地址 里边的值。
示例:
void add(int *val)
{
(*val)++;
}
int main()
{
int a = 1;
add(&a);
printf("%d \n", a);
return 0;
}
运行结果:
上方代码中函数add()将整形指针作为形参,val为指针的变量名, * val为所传地址的值。
通过main函数将a的地址传入函数add()中,在add()函数中,形参val的地址为a的地址, * val为a的值。对a地址内的数据进行加一 (*val)++,也就是a++,最终a = 2。
3. Windows数据存储
X86(32位)环境下运行:
在windows中,数据存放以小端模式(低位数据存放低位地址)
栈区: 当使用编译器时,编译器会自动分配和释放内存,来为运行的程序分配局部变量,函数形参,返回数据,返回地址等等。
地址大小为32位,4字节。
从0x0000 0000 到 0xffff ffff

int main()
{
int a = 1;
int b = 2;
return 0;
}
主函数main()在运行前,已经为变量a,b分配了内存空间。
栈内存划分 由高地址 到 低地址的顺序来划分。
1所在空间分配给a,存放a的值 1。
2分配给b,存放b的值 2。
数组在栈中的存储:
首先根据数组的大小从高地址到低地址开辟出足够的内存空间,再在开辟出的内存空间中按照小端模式,从低地址到高地址依次存放数组中的数据。
数组arr[4] ={1,2,3,4}; 过程如下图
4. 指针的运算
可以通过对指针变量的加减运算来访问不同位置的地址,来得到自己想要的效果。
4.1举例:
4.1.1访问数组
int main()
{
int arr[4] = {
1,2,3,4};
int* p = &arr[0];
for(int i = 0;i<4;i++)
{
printf("%-3d",*(p++));
}
return 0;
}
运行结果:
通过对指针的地址进行 后++ 来依次得到arr数组中的地址,通过解引用来输出地址中的值。
4.1.2将前m个数放到后边
函数Reverse1: 对 begin 到 end 的数据进行逆序 用指针
void Reverse1(int* arr, int begin, int end)//对 begin 到 end 的数据进行逆序 用指针
{
assert(arr != NULL && begin <= end);
int temp = 0;
int* brr = arr;
int len = (end - begin + 1) / 2;
for (int i = 0; i < len; i++, begin++, end--)
{
temp = *(arr + begin);
*(arr + begin) = *(brr + end);
*(brr + end) = temp;
}
}
函数Adjust: 将前m个数放到后边
void Adjust(int* arr, int len, int m) //将前m个数放到后边
{
assert(arr != NULL);
Reverse1(arr, 0, len-1);//对arr整体逆序
Reverse1(arr, 0, len - 1 - m);//对前面len-m个元素进行逆序
Reverse1(arr, len - m, len - 1);//对后m个元素逆序
}
int main()
{
int arr[] = {
1,2,3,4,5,6,7 };
int len = sizeof(arr) / sizeof(arr[0]);
Adjust(arr, len, 4);
for (int i = 0; i < len; i++)
{
printf("%-2d", arr[i]);
}
return 0;
}
运行结果:
边栏推荐
- ARFoundation从零开始9-AR锚点(AR Anchor)
- 最新坦克大战2022-全程开发笔记-1
- C 语言手写 QQ-AI 版
- With frequent data leakage and deletion events, how should enterprises build a security defense line?
- QML type: mousearea
- MySQL的详细安装使用教程(保姆式安装图文讲解)
- C语言函数实现输出I love you
- 7.3-function-templates
- C language handwritten qq-ai version
- QT learning: qdropevent drag event
猜你喜欢

Li Yan, CEO of parallel cloud: cloudxr, opens the channel to the metauniverse

About realizing page Jump of website in Servlet

GPIO的输入输出详解

Live broadcast preview | how to improve enterprise immunity through "intelligent edge security"?

Live broadcast preview | how to save 30% labor cost and shorten 80% trademark processing cycle?

365天挑战LeetCode1000题——Day 039 完全二叉树插入器 + 寻找峰值 II + 快照数组

Unity3d - the object is too far away to see

C how to realize simple factory mode

365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球

The road to success in R & D efficiency of 1000 person Internet companies
随机推荐
Handwritten student management system
阿里云架构师梁旭:MES on 云盒,助力客户快速构建数字工厂
510000 prize pool invites you to fight! The second Alibaba cloud ECS cloudbuild developer competition is coming
ARFoundation从零开始8-Geospatial API(地理空间)开发
Xiaobai high salary shortcut Qt development game Snake
CMake 设置vs启动运行环境路径
Ros1 dead chicken data is stored in txt and SQLite
How to get command parameters in Visual Basic.Net
Complete ecological map of R & D Efficiency & selection of Devops tools
QtCreator+CMake编译器设置
Vs code的安装步骤及环境配置
京东云联合Forrester咨询发布混合云报告 云原生成为驱动产业发展新引擎
Live broadcast preview | how to save 30% labor cost and shorten 80% trademark processing cycle?
webgl1.0下texture2D和texture2DProj区别
AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带
About realizing page Jump of website in Servlet
手写学生管理系统
365天挑战LeetCode1000题——Day 039 完全二叉树插入器 + 寻找峰值 II + 快照数组
With frequent data leakage and deletion events, how should enterprises build a security defense line?
源码编译pytorch坑