当前位置:网站首页>C and pointer Chapter 3 semantic "trap" 3.5 null pointer is not a string
C and pointer Chapter 3 semantic "trap" 3.5 null pointer is not a string
2022-07-29 03:03:00 【weixin_ Guest time】
A null pointer is not an empty string
With one important exception , stay C Language converts an integer to a pointer , The final result depends on the specific C Compiler implementation . This exception is constant 0, The compiler guarantees that 0 The converted pointer is not equal to any valid pointer . For the sake of code documentation , constant 0 This value is often replaced by a symbol :
#define NULL 0
Of course, whether directly using constants 0, Still use symbols NULL, The effect is the same . The important thing to remember is , When constant 0 When converted to pointer , This pointer must not be dereferenced (dereference). let me put it another way , Will be 0 When assigned to a pointer variable , We must not attempt to use the contents stored in the memory pointed to by this pointer .
The following writing is completely legal :
if (p == (char*)0) {
...
}
But if you want to write like this :
if (strcmp(p, (char*)0) == 0) { //illegal behavior
...
}
It's illegal , The reason is that library functions strcmp The implementation of will include an operation , Used to view the contents of memory pointed to by its pointer parameters . If p It's a null pointer , even to the extent that
printf(p); //undefined behavior
and
printf("%s", p); //undefined behavior
The behavior of is undefined . and , Similar statements will have different effects on different computers .
边栏推荐
- [opencv] use OpenCV to call mobile camera
- C陷阱与缺陷 第3章 语义“陷阱” 3.4 避免“举偶法”
- Pytest environment deployment + use case execution management + use case parameterization
- 【FreeSwitch开发实践】UniMRCP编译与安装
- Wechat's crazy use of glide - life cycle learning
- Shell编程规范与变量
- Notes on the sixth day
- 【C】数组
- C陷阱与缺陷 第3章 语义“陷阱” 3.3 作为参数的数组声明
- Notes on the seventh day
猜你喜欢

【C】数组

MySQL - the difference between count (field), count (primary key), count (1), count (*)

Summary of classic problems in Flink production environment

Confusion matrix learning notes

R language error: compilation failed for package '****‘

混淆矩阵学习笔记

Notes on the seventh day

centos安装mysql8

IDEA安装后无法启动

Mongodb index (3)
随机推荐
Flask的创建的流程day05-06之创建项目
从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
MYSQL入门与进阶(十三)
【npm错误】- npm ERR code ERESOLVE 和 npm ERR ERESOLVE could not resolve 问题
C陷阱与缺陷 第3章 语义“陷阱” 3.3 作为参数的数组声明
工科男生:20岁未满,平凡但不平庸
Zone --- line segment tree lazy marking board sub problem
Groupby method
万字详解 Google Play 上架应用标准包格式 AAB
Linux下安装MySQL8.0的详细步骤
C#从网址异步获得json格式的数据
VASP calculation task error: M_ divide:can not subdivide 8 nodes by 6
Verilog's time system tasks - $time, $stime, $realtime
Pytest环境部署+用例执行管理+用例参数化
C语言小项目 -- 通讯录(静态版+动态版+文件版)
2022-07-28 第四小组 修身课 学习笔记(every day)
MYSQL入门与进阶(十二)
【打开新世界大门】看测试老鸟如何把API 测试玩弄在鼓掌之间
Verilog: blocking assignment and non blocking assignment
.NET 序列化枚举为字符串