当前位置:网站首页>Summary of basic knowledge of C language pointer (I)
Summary of basic knowledge of C language pointer (I)
2022-07-26 03:25:00 【BSP primary school monk】
Blog home page :https://blog.csdn.net/weixin_46094737?type=blog
Feel free to leave a comment Please correct any mistakes !
This article was originally written by primary school student Lian , First appeared in CSDN
The future is long , It's worth our effort to go to a better life !
What is a pointer ?
The pointer is a variable , Its value is the address of another variable , namely Direct address of memory location . Just like other variables or constants , Before you can use pointers to store other variable addresses , On the Statement . The general form of pointer variable declaration is :
#include <stdio.h>
int main()
{
int *num_01;
int a=10010;
num_01=&a;
printf(" The pointer num_01 The value of is :%d\n",*num_01);
printf(" The pointer num_01 The address for :%#p",*num_01);
return 0;
} 
ad locum ,int Is the basic type of pointer (int type ), It must be an effective C data type ,num01 Is the name of the pointer variable . Asterisk used to declare pointer * Same asterisk as used in multiplication . however , In this sentence , The asterisk is used to specify that a variable is a pointer .
The actual data type of the value of all pointers , Whether it's an integer 、 floating-point 、 Character , Or other data types , It's all the same , 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 .
Definition and use of pointer variables
1. The definition of a pointer variable
The definition format of pointer variables is :
Type specifier * Pointer variable name [= initial value ];
To define two pointer variables at the same time :
int *p1=NULL,*p2=NULL;
(NULL yes c Predefined null pointer keywords in language , It acts as a special pointer , Indicates that it does not point to any object )
Pay attention to the following points when defining pointer variables :
(1) The type specifier indicates the data type of the variable pointed to by the pointer variable .
(2) When defining pointer variables , Pointer variable name must be preceded by “ * ” Number , Indicates that the defined variable is a pointer variable .
(3) Pointer variables are allowed to be initialized when they are defined .
2. The use of pointer variables
Two important operators :
(1)&: Fetch address operator , Such as p=&a; be p As a variable a The address of .
(2): The pointer Operator , Only pointer variables can be followed . Used to access the variable pointed to by the pointer variable . Such as :*p Indicates access to pointer variables p The content of the variable pointed to .
Practice using pointers to exchange the addresses and values of two variables
#include <stdio.h>
void temp(int *num01,int *num02);
int main()
{
int a=100,b=200,c;
int *p=NULL;
int *pa=&a;
int *pb=&b;
printf(" Before value exchange :\n");
printf("%d\n",a);
printf("%d\n",b);
printf(" Before address exchange :\n");
printf("%p\n",pa);
printf("%p\n",pb);
p=pa;
pa=pb;
pb=p;
printf(" After address exchange :\n");
printf("%p\n",pa);
printf("%p\n",pb);
temp(&a,&b);// Use function exchange
printf(" After value exchange :\n");
printf("%d\n",a);
printf("%d\n",b);
return 0;
}
void temp(int *num01,int *num02)
{
int p;
p=*num01;
*num01=*num02;
*num02=p;
}

Pointers and arrays
One 、 Pointer to a one-dimensional array element
c In the language array Array elements can be regarded as variables of corresponding types . As long as the type matches , You can define a pointer variable , Let this variable store the address of the array element in the array , Then the pointer points to the array element .
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Definition a For an array containing ten data
int *p; //p Is a pointer variable to an integer variable
p = &a[0];// hold a[0] The address of the element is assigned to a pointer variable p
p=a;// And p = &a[0] equivalent ,p Is the value of an array a The address of the first element Reference one-dimensional array elements through pointers :
1. Pointer to the first element of a one-dimensional array
Be careful !!!
(1) have access to *(p+i) Access elements a[i].
(2) because p and a Both represent the first address of the array , therefore p+i It can also be recorded as a+i, Point to elements a[i].
(3) Pointer variables pointing to arrays can also have subscripts , Such as :p[i] And *(p+i) and *(a+i) Equivalent , Show elements a[i].
Thus we can see that : When a pointer variable p Point to a one-dimensional array a, That is to point to the first element of a one-dimensional array a[0] after , The first of an array of i+1 The elements have the following 4 Species writing :
a[i] ; p[i] ; *(a+i) ; *(p+i)
a[i] The address of also corresponds to 4 Species writing :
&a[i] ; &p[i] ;a+i ; p+i ;
#include <stdio.h>
int main()
{
int *p;
int arr[5]={1,2,3,4,5};
int i;
p=&arr[0];// Point to the first address arr[0]
printf(" The value of the first address of the array element is :%d\n",*p);
p=arr;// You can also define pointers like this p The first address of the array pointed to
printf(" The pointer p The address for :%#p\n",p);
for(i=0;i<5;i++)
{
printf("%d ",*p);
p++;
}
return 0;
}
边栏推荐
- 2022-07-21 第四小组 修身课 学习笔记(every day)
- Canvas -- draw text -- modification of pie chart
- Derivation of linear regression principle
- What is the difference between heap memory and stack memory?
- LeetCode·
- Usage of tf.variable() function in tensorflow
- els 初始化窗口类
- tensorflow中tf.Variable()函数的用法
- [MySQL project practical optimization] complex trigger case sharing
- Cloud native guide what is cloud native infrastructure
猜你喜欢

Uncaught TypeError: $(...).onmouseenter is not a function js错误,解决办法:

c语言指针基本知识要点总结(一)

Opencv annotates the image (picture frame + writing)

canvas——绘制文本——饼图的修改

Sentinel vs Hystrix 到底怎么选?

LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序

Looking at the next step of BAIC bluevale through the 8billion fund-raising, product upgrading and building core capabilities are the key words

easyExcel设置行隐藏,解决setHidden(true)失效问题

Intensive reading of the paper -yolov1:you only look once:unified, real time object detection

Offline data warehouse from 0 to 1-stage II software installation
随机推荐
Opencv saves pictures in the specified format
URDF syntax explanation
Cloud native guide what is cloud native infrastructure
Hcip day 8 notes sorting (OSPF routing control, Appendix E, anti ring, shortest path calculation, republication))
Canvas -- drawing of rectangle -- making of histogram
文件上传报错:Current request is not a multipart request
Implement a method to find the sum of the number k and m in the array
Opencv 在图像上进行标注(画框+写字)
Canvas -- draw text -- modification of pie chart
"Xiao Deng's view" the value brought by Siem to enterprises (II)
ext4、ntfs、xfs、btrfs、zfs、f2fs和reiserFS性能对比
LeetCode·
ELS callback function, exit message
c语言指针基本知识要点总结(一)
PXE efficient batch network installation
LoRa无线网关如何快速实现端到云的传输
Performance comparison of ext4, NTFS, XFS, Btrfs, ZFS, f2fs and ReiserFS
How Lora wireless gateway can quickly realize end-to-cloud transmission
Why did Mr. Cao, a productionist in the field of high-end tea, choose Ruifeng L6 max?
[stl] priority queue priority_ queue