当前位置:网站首页>9. Pointer (upper)
9. Pointer (upper)
2022-07-06 13:26:00 【It's Wang Jiujiu】
Catalog
5. The operation of the pointer
The operational relationship of pointers
1. What is the pointer ?
The pointer ? What is a pointer ? fundamentally , The pointer (pointer) Is a variable whose value is the memory address ( Or data objects ). just as char The value of a type variable is a character ,int The value of a type variable is an integer , The value of the pointer variable is the address . Pointer in spoken language usually refers to pointer variable , The variable used to hold the address .
int main()
{
int num = 10;
int* p = #
printf("%p\n", p);
return 0;
}- In the above code ,& To get the address symbol , Put the variable num Take out the address of and assign it to p.
- * Indicates that the declared variable is a pointer , and int Indicates that the type of pointer is integer ;int*p It means ,p It's a pointer ,*p yes int type .
- utilize printf When the function prints the address ,%p The corresponding address is .
With X86 Run the above code in the environment , give the result as follows :

009BFA40 In order to 16 In base notation , Variable num It is stored in this address .
Just like the address in our life ,C The address in the language is also unique .
2. The size of the pointer
Since the pointer is used as a variable to store the address , Then the computer will certainly open up a space for storing pointers .
Take the above code as an example , adopt sizeof You can calculate the size of the pointer :
int main()
{
int num = 10;
int* p = #
printf("%d\n", sizeof(p));
return 0;
}stay X86 In the environment , namely 32 In the machine : Pointer 4 Bytes .
stay X64 In the environment , namely 64 In the machine : Pointer 8 Bytes .
1 Bytes are equal to 8 A bit , that 32 In the machine , The pointer can indicate 2^32 An address ;64 In a bit machine , Can represent 2^64 An address .
The smallest unit in the pointer is a byte , This is after careful thinking and weighing . That is, we can query a char Address of type character , But you can't query the address of a bit in this character .
3. The type of pointer
When creating a variable , We need to declare the type for it , for example : Use integer to express age ,float Type indicates height 、 Weight etc. ; The same is true when creating pointers , For different types of variables , Declare a pointer of the corresponding type to store its address .
char *pc = NULL;
int *pi = NULL;
short *ps = NULL;
long *pl = NULL;
float *pf = NULL;
double *pd = NULL;(* And pointer name , Not affecting use )
Pointer dereference (*)
Through the indirect operator *(indirection operator) You can use the address to change the variable , This operation is also called “ Quoting ”.
#include<stdio.h>
int main()
{
int num = 10;
printf(" Before change :%d\n", num);
int* p = #
*p = 2;
printf(" After change :%d\n", num);
return 0;
}
We have not changed directly num Value , It's about finding num Storage address , Through dereference operation, it indirectly changes num Value .
The pointer +- Integers
#include<stdio.h>
int main()
{
int num = 10;
int* pi = #
char* pc = (char*)#
printf("%p\n", &num);
printf("%p\n", pi);
printf("%p\n", pi + 1);
printf("%p\n", pc);
printf("%p\n", pc + 1);
return 0;
}
expression char* pc = (char*)&num: Will integer variable num Take out your address , Cast to char* type , Then give to char* Pointer variable of type pc.
Found by test : The pointer type corresponds to the pointer +- Time span , namely char Pointer to type +1 Visit later 1 Bytes ,int Pointer to type +1 Visit later 4 Bytes .
4. Wild pointer
Concept : The position of the pointer is unknown ( Random 、 incorrect 、 There is no definite limit to )
The cause of the wild pointer
Pointer not initialized
#include <stdio.h>
int main()
{
int* p;
*p = 10;
return 0;
}Local variable pointer not initialized , The default value is random .
Pointer cross boundary access
#include <stdio.h>
int main()
{
int arr[10] = { 0 };
int* p = arr;
int i = 0;
for (i = 0; i < 11; i++)
{
*(p++) = i;
}
return 0;
}Array arr The subscript of is 0~9, When the pointer points to an array arr The scope of time ,p It's a wild pointer .
The space that the pointer points to is released
When the variable pointed to by the pointer is released , The pointer is a wild pointer .
Avoid wild pointer
1. Pointer initialization
2. Watch out for the pointer
3. Pointer to space release even if set to NULL
4. Avoid returning the address of a local variable
5. Check the validity of the pointer before using it (avoid)
#include <stdio.h>
#include <assert.h>
int main()
{
int* p = NULL;
int a=10;
int* p = &a;
assert(p != NULL);
*p = 10;
if(p!=NULL)
*p = 20;
return 0;
}
5. The operation of the pointer
The operational relationship of pointers
#include<stdio.h>
#define N 5
int main()
{
int ch[N] = { 0 };
int* vp;
for (vp = &ch[0]; vp < &ch[N];)
{
*vp++ = 1;
}
return 0;
}- expression vp = &ch[0] Indicates that the address of the first element in the array is assigned to vp.
- vp < &ch[N] It means that you will vp The address stored in the array is compared with the address of the next element of the array , as long as vp Less than it , It means that it is still accessed in the array , No boundaries .
- *vp++ It's actually *(vp++), One integer per backward access .
The pointer - The pointer
#include<stdio.h>
int my_strlen(char* s)
{
char* p = s;
while (*p != '\0')
p++;
return p - s;
}
int main()
{
char ch[]="hello";
printf("%d\n", my_strlen(ch));
return 0;
}The standard stipulates
Allows a pointer to an array element to be compared with a pointer to the memory location after the last element of the array , However, it is not allowed to compare with a pointer to the memory location before the first element .
6. Pointers and arrays
This part is dedicated to a chapter , Please have a look at Array 、 The relationship between pointer and array .
7. The secondary pointer
Pointer variables are also variables , If it is a variable, it must have a storage address . Then the pointer that stores the address of the pointer variable , It is called secondary pointer .
#include<stdio.h>
int main()
{
int a = 10;
int* p1 = &a;
int** p2 = &p1;
**p2 = 20;
return 0;
}8. Pointer array
Pointer arrays are arrays , It's just an array for storing pointers .
#include<stdio.h>
int main()
{
int arr1[] = {1,2,3};
int arr2[] = {4,5,6};
int arr3[] = {7,8,9};
int* p1 = arr1;
int* p2 = arr2;
int* p3 = arr3;
int* p[] = { p1,p2,p3 };
return 0;
}int* p[] = { p1,p2,p3 } Indicates that the type of data stored in the array is int* type , The stored data are p1,p2,p3.
int i = 0, j = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", *(*(p + i))+j);
}
putchar('\n');
}
Set up two for Loop print pointer array p, It can be found that one-dimensional pointer array realizes the effect of two-dimensional array .
边栏推荐
- 4.30动态内存分配笔记
- string
- 十分鐘徹底掌握緩存擊穿、緩存穿透、緩存雪崩
- Pit avoidance Guide: Thirteen characteristics of garbage NFT project
- Tyut outline of 2022 database examination of Taiyuan University of Technology
- 阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
- Branch and loop statements
- 3.C语言用代数余子式计算行列式
- Abstract classes and interfaces
- 3.猜数字游戏
猜你喜欢

IPv6 experiment

Wei Pai: the product is applauded, but why is the sales volume still frustrated

String class

图书管理系统小练习

Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology

更改VS主题及设置背景图片

2.C语言初阶练习题(2)

13 power map

Experience summary of autumn recruitment of state-owned enterprises

Common method signatures and meanings of Iterable, collection and list
随机推荐
Redis cache obsolescence strategy
TYUT太原理工大学2022数据库考试题型大纲
IPv6 experiment
167. Sum of two numbers II - input ordered array - Double pointers
2.C语言初阶练习题(2)
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
IPv6 experiment
MPLS experiment
Introduction pointer notes
Alibaba cloud microservices (I) service registry Nacos, rest template and feign client
CorelDRAW plug-in -- GMS plug-in development -- Introduction to VBA -- GMS plug-in installation -- Security -- macro Manager -- CDR plug-in (I)
FileInputStream和BufferedInputStream的比较
阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
System design learning (III) design Amazon's sales rank by category feature
[中国近代史] 第九章测验
Inheritance and polymorphism (I)
There is always one of the eight computer operations that you can't learn programming
View UI plus released version 1.3.0, adding space and $imagepreview components
Relational algebra of tyut Taiyuan University of technology 2022 database