当前位置:网站首页>C language: 6. Simple use and precautions of pointer
C language: 6. Simple use and precautions of pointer
2022-07-27 19:33:00 【Brother rabbit head】
c Language :6、 Simple use and precautions of pointer
Content sources :https://www.runoob.com/cprogramming/c-pointers.html
1、 Simple instance of pointer
Through the pointer , It can be simplified C Execution of programming tasks , There are other tasks , Such as dynamic memory allocation , Can't execute without a pointer .
Each variable has a memory location , Each memory location defines the available memory & Address accessed by operator , It represents an address in memory
#include <stdio.h>
int main()
{
int num = 10;
int *p; // Defining pointer variables
p = #
printf("num The address of the variable :%p\n", p);
return 0;
}
The compilation and execution results of the above code are as follows :
num The address of the variable :0x7ffeeaae08d8
2、 What is a pointer
The pointer is the memory address , Pointer variables are variables used to store memory addresses .
Just like using other variables or constants , Before using pointers to store other variable addresses , It must be declared . The format is as follows :
// data type * Pointer variable name ;
int *ip; /* Pointer to an integer */
double *dp; /* One double Pointer to type */
float *fp; /* A floating point pointer */
char *ch; /* A character pointer */
All actual data types , Whether it's an integer 、 floating-point 、 Character , Or other data types , The values of the corresponding pointers are of the same type , Is a long hexadecimal number representing the memory address .
The only difference between pointers of different data types is , The data type of the variable or constant that the pointer points to is different .
3、 How to use the pointer
The following operations are frequently performed when using pointers :
- Define a pointer variable , Such as
int *p; - Assign variable address to pointer ,
p = # - Access the value of the address available in the pointer variable ,
int val = *p;
#include <stdio.h>
int main()
{
int var = 20; // Actual variable declaration , Definition
int *ip; // Declaration of pointer variables
ip = &var; // Store... In pointer variable var The address of
printf("var The address of the variable :%p\n", &var);
// Address stored in pointer variable
printf("ip The address of the variable store : %p\n", ip);
// Use pointers to access values
printf("*ip The value of the variable :%d\n", *ip);
}
The above code compiles and executes , give the result as follows :
var The address of the variable : 0x7ffeeef168d8
ip The address of the variable store : 0x7ffeeef168d8
*ip The value of the variable : 20
4、NULL The pointer
When declaring pointer variables , If there is no explicit address to assign , Assign a value of NULL Is a good programming habit .
To assign as NULL A pointer to a value is called a null pointer .
NULL Pointer is a constant defined in the standard library with a value of zero .
#include <stdio.h>
int main()
{
int *ptr = NULL;
printf("ptr The address is :%p\n", ptr);
return 0;
}
Run the above code and the result is :
ptr The address is 0x0
On most operating systems , The program does not allow access to address 0 Of memory , Because the memory is reserved by the operating system . then , Memory address 0 It is of great significance , It indicates that the pointer does not point to an accessible memory address . But by convention , If the pointer contains a null value ( Zero value ), Suppose it doesn't point to anything .
Check null pointer mode :
if (ptr) // If ptr Non empty , Then finish
if (!prt) // If ptr It's empty , Then finish
5、 Pointer notes
5.1、 Do not hard code assignment to pointer
The following code assigns a value to a pointer by hard coding , It's a very dangerous thing ( This method has some usage scenarios , It is mainly reflected in the scenario that the starting address of embedded programming and other programs are written dead or the communication address is written dead ).
int *p = (int *)0XFDDFFBD4;
PRINT_HEX(p);
PRINT_INT(*p);
5.2、 Be careful to avoid generating wild pointers
int *point_bad = NULL;
void badPointDemo(){
int a = 10;
point_bad = &a;
//....do some thing
//1、 When badPointDemo() When the function finishes running , Variable a The memory address of will be recycled .
//2、 At this time, if the pointer is not set to NULL,point_bad The variable becomes a wild pointer
//3、 The wild pointer cannot be judged by any method
point_bad = NULL;
}
int main(){
badPointDemo();
// Because the pointer variable is set to NULL 了
// So you can be right point_bad Pointer to judge
if(point_bad){
}
return 0;
}
边栏推荐
- c语言:14、预处理
- 汉字查拼音微信小程序项目源码
- Kinect for Unity3d----KinectManager
- Latex use - control the display position of tables or graphics
- c语言:c语言代码风格
- Kettle JVM memory setting - the effect is not obvious
- win10小技巧(1)——转移桌面位置
- SQL time processing (SQL server\oracle)
- Automatic testing of Web UI: Selenium syntax explanation is the most complete in the history
- c语言:5、多维数组
猜你喜欢

Mongodb learning notes (1) - install mongodb and its related configurations

c语言:clion调试方法

阿里云视频点播服务的开通和使用

Down sampling - signal phase and aliasing

idea优化小攻略

200 lines of code quick start document database monogodb

Rs2022/ cloud detection: semi supervised cloud detection in satellite images by considering the

Take byte offer in four rounds and answer the interview questions

c语言:c语言代码风格

c语言:5、多维数组
随机推荐
c语言:9、main函数中的return
MySQL学习笔记(2)——存储过程与存储函数
go-zero单体服务使用泛型简化注册Handler路由
VMware: set up SSH
4 轮拿下字节 Offer,面试题复盘
mysql学习笔记(1)——变量
Programming jump
成本高、落地难、见效慢,开源安全怎么办?
200行代码快速入门文档型数据库MonogoDB
2022 preparation for autumn recruitment 10W word interview sketch PDF version, with operating system and computer network interview questions
kettle EXCEL 累计输出数据
IPFs obtains the public key and private key through the interface, and encrypts the storage. First bullet
c语言:12、gdb工具调试c程序
时间复杂度和空间复杂度
Matrix of shell programming -- it's cute and cool
kettle JVM内存设置---效果不明显
Web3.0热浪之下 门萨Mensa生态震撼来袭
Latex use - control the display position of tables or graphics
[Luogu p4183] cow at large P (graph theory) (tree array)
The go zero singleton service uses generics to simplify the registration of handler routes