当前位置:网站首页>指针
指针
2022-07-29 05:08:00 【Ryan菲特】
指针
查找地址:&运算符
指针是C语言最重要的(有时也是最复杂的)概念之一,用于存储变量的地址。在使用scanf()时就使用了地址作为参数。
主调函数不使用 return 返回的值,则必须通过地址才能修改主调函数中的值。
一元 & 运算符的用法:
一元 & 运算符给出变量的存储地址。如pooh是变量名,则 &pooh是变量的地址,可以把地址看做是变量在内存中的位置。
如语句:printf("%d %p",pooh,&pooh);该语句就会输出两个值一个是pooh的值,一个是pooh值的地址。
//查看变量被存储在何处
#include <stdio.h>
void mikado(int);//函数原型
int main(void)
{
int pooh = 2, bah = 5;//main()的局部变量
printf("In main() ,pooh = %d and &pooh = %p\n", pooh, &pooh);
printf("In main() ,bah = %d and &bah = %p\n", bah, &bah);
mikado(pooh);
return 0;
}
void mikado(int bah)
{
int pooh = 10;//mikado()局部变量
printf("In mikado(), pooh = %d and &pooh = %p\n", pooh, &pooh);
printf("In mikado(), bah = %d and &bah = %p\n", bah, &bah);
}
输出结果:
In main() ,pooh = 2 and &pooh = 000000000061fe18
In main() ,bah = 5 and &bah = 000000000061fe1c
In mikado(), pooh = 10 and &pooh = 000000000061fddc
In mikado(), bah = 2 and &bah = 000000000061fdf0以上结果可以看出虽然名称相同但是局部变量的地址都是不一样的。
间接运算符(解引用运算符):*
假设已知 ptr 指向bah,如:ptr = &bah;
使用 * 运算符可以找出存储在bah中的值。语句 ptr = &bah; 和val = *ptr;放在一起就相当于
val = bah;
总结以上两点:
与指针相关的运算符:
地址运算符: &
后跟一个变量名时,&给出该变量的地址。示例:&nurse表示变量nurse的地址。
间接(或解引用)运算符 :*
后跟一个指针名或地址时,*给出存储在指针指向地址上的值。示例:
nurse = 22;
ptr = &nurse;//指向nurse的指针
val = *ptr;//把ptr指向的地址上的值赋给val执行以上3条语句的最终结果是把22赋给val。
声明指针需注意:
1.声明指针变量时必须指定指针所指向变量的类型,因为不同的变量类型占用不同的存储空间,一些指针操作要求知道操作对象的大小。其次程序必须知道存储在指定地址上的数据类型。
主要声明方式有以下几种:
int *pi; //pi是指向int类型变量的指针(表示pi是一个指针,*pi是int类型)
char *pc; //pc是指向char类型变量的指针
float *pf, *pg;//pf、pg都是指向float类型变量的指针2.不要把指针指针认为是整数类型,一些处理整数的操作不能用来处理指针,反之也是。指针实际上是一个新类型,为此专门提供了%p格式的转换说明。
指针在函数中的使用:
//使用指针解决交换函数的问题
#include <stdio.h>
void interchange(int * u ,int * v);
int main(void)
{
int x = 5, y = 10;
printf("Original x = %d and y = %d.\n", x, y);
interchange(&x, &y);//把变量的地址发个函数
printf("Now x = %d and y = %d\n", x, y);
return 0;
}
void interchange(int * u ,int * v)
{
int temp;
temp = *u;//temp获取u所指向对象的值
*u = *v;
*v = temp;
}以上代码实现了函数返回了两个变量的值。
注意点:
1.指针的值是它所指向对象的地址。地址的表示方式依赖于计算机内部的硬件。许多计算机(包括PC和Macintosh)都是按字节编址,意思是内存中的每个字节都是按照顺序编号。
2.在指针前面使用 * 运算符可以得到该指针所指向对象的值。
3.指针加1,指针的值递增它所指向类型的大小(以字节为单位)例如:short类型占用2字节则指针+1就是指针的值递增2。
边栏推荐
- CMake 设置vs启动运行环境路径
- Webrtc audio anti weak network technology (Part 2)
- 递归的基本原理
- MFC集成qt验证及问题处理
- 副作用和序列点
- C语言用指向指针的指针对n个整数排序
- MySQL的详细安装使用教程(保姆式安装图文讲解)
- Best practices of JD cloud Distributed Link Tracking in financial scenarios
- Modification of annotation based three-tier project and the way of adding package scanning
- ARFoundation入门教程7-url动态加载图像跟踪库
猜你喜欢

OCCT学习001-----简介

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

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

About realizing page Jump of website in Servlet

2022年泰迪杯数据挖掘挑战赛C题方案及赛后总结

阿里云架构师梁旭:MES on 云盒,助力客户快速构建数字工厂

C how to realize simple factory mode

Apache POI implements excel import, read data, write data and export

QT series - Installation

串口通讯部分详解
随机推荐
QML定制TabBar
Handwritten student management system
Arfoundation starts from zero 9-ar anchor
CryEngine5 Shader调试
Unity3D - 物体太远看不见的问题
Getting started with solidity
365天挑战LeetCode1000题——Day 037 元素和小于等于阈值的正方形的最大边长 + 满足条件的子序列数目
如视技术副总裁杨永林:当传统产业遇到“数字空间”
GPIO的输入输出详解
Yangyonglin, vice president of Rushi Technology: when traditional industries encounter "digital space"
数据泄漏、删除事件频发,企业应如何构建安全防线?
【[第一次写博客]Uda课程中的P控制器实现说明】
About the configuration and use of thymeleaf
osgSimplegl3例子分析
Custom QML control: imagebutton
200 多家 ISV 入驻!阿里云计算巢发布一周年
C how to realize simple factory mode
Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com
QtCreator+CMake编译器设置
OCCT学习002-----环境搭建