当前位置:网站首页>c语言:6、指针的简单使用与注意事项
c语言:6、指针的简单使用与注意事项
2022-07-27 16:47:00 【兔头哥哥】
c语言:6、指针的简单使用与注意事项
内容来源:https://www.runoob.com/cprogramming/c-pointers.html
1、指针简单实例
通过指针,可以简化一些 C 编程任务的执行,还有一些任务,如动态内存分配,没有指针是无法执行的。
每一个变量都有一个内存位置,每一个内存位置都定义了可使用&运算符访问的地址,它表示了在内存中的一个地址
#include <stdio.h>
int main()
{
int num = 10;
int *p; //定义指针变量
p = #
printf("num变量的地址:%p\n", p);
return 0;
}
上方代码编译执行结果如下:
num变量的地址:0x7ffeeaae08d8
2、什么是指针
指针也就是内存地址,指针变量是用来存放内存地址的变量。
像使用其他变量或常量一样,在使用指针存储其他变量地址之前,必须对其进行声明。格式如下:
//数据类型 *指针变量名;
int *ip; /* 一个整型的指针 */
double *dp; /* 一个 double 型的指针 */
float *fp; /* 一个浮点型的指针 */
char *ch; /* 一个字符型的指针 */
所有实际数据类型,不管是整型、浮点型、字符型,还是其他数据类型,对应指针的值的类型都是一样的,都是一个代表内存地址的长的十六进制数。
不同数据类型的指针之间唯一的不同是,指针所指向的变量或常量的数据类型不同。
3、如何使用指针
使用指针时会频繁进行以下几个操作:
- 定义一个指针变量,如
int *p; - 把变量地址赋值给指针,
p = # - 访问指针变量中可用地址的值,
int val = *p;
#include <stdio.h>
int main()
{
int var = 20; //实际变量声明,定义
int *ip; //指针变量的声明
ip = &var; //指针变量中存储 var 的地址
printf("var 变量的地址:%p\n", &var);
//在指针变量中存储的地址
printf("ip 变量存储的地址: %p\n", ip);
//使用指针访问值
printf("*ip 变量的值:%d\n", *ip);
}
上方代码编译执行,结果如下:
var 变量的地址: 0x7ffeeef168d8
ip 变量存储的地址: 0x7ffeeef168d8
*ip 变量的值: 20
4、NULL指针
声明指针变量时,若无明确的地址可赋值,为指针变量赋值为NULL是一个良好的编程习惯。
赋为NULL值的指针称为空指针。
NULL指针是一个定义在标准库中的值为零的常量。
#include <stdio.h>
int main()
{
int *ptr = NULL;
printf("ptr 的地址是:%p\n", ptr);
return 0;
}
运行上方代码结果为:
ptr 的地址是 0x0
大多数操作系统上,程序不允许访问地址为 0 的内存,因为该内存是操作系统保留的。然后,内存地址 0 有特重要的意义,它表明该指针不指向一个可访问的内存地址。但按照惯例,如果指针包含空值 (零值),则假定它不指向任何东西。
检查空指针方式:
if (ptr) //如果 ptr 非空,则完成
if (!prt) //如果 ptr 为空,则完成
5、 指针注意事项
5.1、不要硬编码赋值给指针
下方代码使用硬编码的方式把值赋值给一个指针,这是一件很危险的事情(此方式有一些使用的场景,主要体现在嵌入式编程等程序起始地址写死或者通讯地址写死的场景)。
int *p = (int *)0XFDDFFBD4;
PRINT_HEX(p);
PRINT_INT(*p);
5.2、注意避免产生野指针
int *point_bad = NULL;
void badPointDemo(){
int a = 10;
point_bad = &a;
//....do some thing
//1、当badPointDemo()函数运行完成时,变量a的内存地址会进行回收。
//2、此时若不将指针置为NULL,point_bad变量就变成一个野指针了
//3、野指针是无法通过任何方法进行判断的
point_bad = NULL;
}
int main(){
badPointDemo();
//因为上方函数中将指针变量给设为NULL了
//所以可以对point_bad指针进行判断
if(point_bad){
}
return 0;
}
边栏推荐
- Some advice for NS2 beginner.
- Browser rendering principle analysis suggestions collection
- To create a MySQL data source resource group, you must choose to create a new exclusive data integration resource group? Or use a common resource group? thank you
- Matrix of shell programming -- it's cute and cool
- Kettle consolidated record data reduction
- kettle 分列、合并记录
- [cloud picture theory] the first time to know Huawei cloud micro service engine CSE in issue 250
- MySQL learning notes (1) -- variables
- Nacos cluster deployment - high availability guarantee
- Latex use - subfigure vertical graphics
猜你喜欢

Kettle8.2 installation and common problems

go-zero单体服务使用泛型简化注册Handler路由

There is a problem with the time zone when the idea connects to the database. The server returns invalid timezone is red Need to set ‘serverTimezone‘ property.

Cumulative output data of kettle Excel

Using vscode to build u-boot development environment

5W奖金池/面向高校,2022法律科技创新大赛报名火热进行中

kettle引用外部脚本完成电话号码清洗、去重缩进

Kettle consolidated record data reduction

idea优化小攻略

MySQL学习笔记(2)——存储过程与存储函数
随机推荐
Basic knowledge of C language (for personal use)
ES6 new method
MySQL learning notes (1) -- variables
每日一题(02):倒置字符串
Down sampling - signal phase and aliasing
VMware: set up SSH
Unity shows Kinect captured shots
C # one method returns multiple values. Suggestions collection
请问创建MySQL数据源资源组必须要选择新建独享数据集成资源组才可用?还是使用公共资源组就可以?谢谢
Browser rendering principle analysis suggestions collection
2022 Ningde Vocational College Teachers' practical teaching ability improvement training - network construction and management
FZU1669 Right-angled Triangle【毕达哥拉斯三元组】
An article allows you to master threads and thread pools, and also solves thread safety problems. Are you sure you want to take a look?
低代码实现探索(四十五)业务参数
ES6学习笔记(1)——快速入门
Analysis of Eureka server
让你的聊天气泡丰富多彩
汉字查拼音微信小程序项目源码
搭建阿里云+typora+Picgo图床错误分析
The go zero singleton service uses generics to simplify the registration of handler routes