当前位置:网站首页>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;
}
边栏推荐
- Nacos集群部署-高可用保证
- Definition of graph traversal and depth first search and breadth first search (2)
- 每日一题(02):倒置字符串
- Electromagnetic field learning notes - vector analysis and field theory foundation
- 阿里云对象存储OSS的开通和使用
- Word 2007+ tips
- C language preprocessing instruction
- Yanghui triangle
- sql 时间处理(SQL SERVER\ORACLE)
- Learning notes of Microcomputer Principles - common addressing methods
猜你喜欢

Opening and using Alibaba cloud object storage OSS

MongoDB学习笔记(1)——安装MongoDB及其相关配置

Kettle JVM memory setting - the effect is not obvious

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

MySQL learning notes (2) -- stored procedures and stored functions

idea优化小攻略

成本高、落地难、见效慢,开源安全怎么办?

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

Subscription and use of Alibaba cloud video on demand service

Technology Summit 58 Liu Yuan in the same city was invited to attend qecon 2022 global software quality & effectiveness conference
随机推荐
Rs2022/ cloud detection: semi supervised cloud detection in satellite images by considering the
WSN journal indexed by SCI
Double insurance for line breaking
Programming jump
C语言案例:密码设置及登录> 明解getchar与scanf
阿里云对象存储OSS的开通和使用
New system installation mysql+sqlyog
Unity display Kinect depth data
每日一题(02):倒置字符串
MongoDB学习笔记(1)——安装MongoDB及其相关配置
c语言:13、指针与内存
让你的聊天气泡丰富多彩
Introduction to assembly language (1)
Unity learning notes (rigid body physics collider trigger)
Kettle JVM memory setting - the effect is not obvious
[Luogu p4183] cow at large P (graph theory) (tree array)
SQL Server top keyword usage
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?
Questions about webservice
C # interaction with MySQL database - MySQL configuration and addition, deletion, query and modification operations