当前位置:网站首页>[dish of learning notes dog learning C] initial level of pointer
[dish of learning notes dog learning C] initial level of pointer
2022-07-24 10:35:00 【Jiang Junzhu】
List of articles
What is the pointer
A pointer is relative to a memory unit , Refers to the address of the unit , The content of the unit contains data . stay C In language , Allow pointer variables to hold pointers , therefore , The value of a pointer variable is The address of a memory unit .
Pointers describe the location of data in memory , Indicates an entity occupying storage space , The relative distance value of the starting position in this space .
Pointer types
The meaning of pointer type :
1. The pointer type determines the permission of pointer dereference ;
2. The type of pointer determines , The pointer takes one step , How far can I go ( step ).
int main() {
int a = 0x11223344;
int* pa = &a;
char* pc = &a;
printf("%p\n", pa);
printf("%p\n\n", pa + 1);
printf("%p\n", pc);
printf("%p\n\n", pc + 1);
printf("%0x\n", a);
*pc = 0;
printf("%0x\n", a);
*pa = 0;
printf("%0x\n", a);
return 0;
}
Wild pointer
A wild pointer means The position pointed by the pointer is unknown ( Random 、 incorrect 、 There is no definite limit to ).
1. Pointer not initialized
int main() {
// Pointer not initialized
int* p;//p Is a local pointer variable , Local variables are not initialized , The default is a random value
*p = 10;
return 0;
}
2. Pointer access is out of bounds
int main() {
// Pointer access is out of bounds
int arr[10] = {
0 };
int* pa = arr;
int i = 0;
for (i = 0; i <= 10; i++) {
*pa = i;
pa++;
}
return 0;
}
3. Pointer to space release
int* test() {
int a = 10;
return &a;
}
int main() {
// Pointer to space release
int* p = test();
*p = 20;
return 0;
}
Avoid wild pointer :
1. Pointer initialization - Clearly know the initialization value
2. Watch out for the pointer
3. The pointer points to the space release and is set to NULL
4. Check the validity of the pointer before using it
Pointer arithmetic
The pointer +- Integers ;
The pointer - The pointer , obtain Number of elements between two pointers ; The premise of pointer subtraction is that two pointers point to The same space ;
Pointer relation operation
The standard specifies that the pointer to the array element can be the same as the pointer to Pointer to the memory location after the last element of the array / Address Compare , But it is not allowed to point to Pointer to the memory location before the first element / Address Compare .
int main() {
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
int* p = arr;
int* pend = arr + 9;
while (p <= pend) {
printf("%d\n", *p);
p++;
}
printf("%d\n", &arr[9] - &arr[0]);
return 0;
}
int my_strlen(char* str) {
char* start = str;
while (*str != '\0') {
str++;
}
return str - start;
}
int main() {
int len = my_strlen("abc");
printf("%d\n", len);
return 0;
}
Arrays and pointers
int main() {
int arr[10] = {
1,2,3,4,5,6,7,8,9,10 };
//[] Is an operator ,2 and arr Are two operands , The order of the two operands can be exchanged
//arr[2] It will be converted to *(arr+2),arr[2] <==> *(arr+2)
//arr Equivalent p,*(arr+2) <==> *(p+2) <==> *(2+p) <==> *(2+arr)
//arr[2] <==> *(arr+2) <==> *(p+2) <==> *(2+p) <==> *(2+arr) <==> 2[arr]
//arr[2] --> *(arr+2) -->*(2+arr) --> 2[arr]
int* p = arr;
printf("%d\n", arr[2]);
printf("%d\n\n", p[2]);
printf("%d\n", 2[arr]);
printf("%d\n", arr[2]);
return 0;
}
Pointer array
An array of pointers is essentially an array , Is an array specially used to store pointers .
The secondary pointer
The essence of pointer is also a variable , Since it's a variable , Then it will also be stored in memory , There will also be a storage address , The variable used to store the address of a primary pointer is called a secondary pointer . Similarly, the variable used to store the secondary pointer address is the tertiary pointer ( No doll ,doge).
int main() {
int a = 10;
int* pa = &a;
//ppa It's just a secondary pointer variable
int** ppa = &pa;//pa It's also a variable ,&pa Take out pa Start address in memory
return 0;
}
【 Learning notes Dog learn C】 Getting to know the pointer
【 Learning notes Dog learn C】 Advanced pointer
边栏推荐
- NLP introduction + practice: Chapter 2: introduction to pytorch
- Google Earth engine - QA in Landsat 5 toa dataset_ Pixel and QA_ Radsat band
- Kotlin domain specific language (DSL)
- PC博物馆(1) 1970年 Datapoint 2000
- 脚手架文件目录说明、文件暴露
- Scope usage in POM file dependency
- MySQL - normal index
- 火山引擎:开放字节跳动同款AI基建,一套系统解决多重训练任务
- Array element removal problem
- Nirvana rebirth! Byte Daniel recommends a large distributed manual, and the Phoenix architecture makes you become a God in fire
猜你喜欢
![[correcting Hongming] what? I forgot to take the](/img/41/c8fa6380ab63949ae6d904fdbb005c.png)
[correcting Hongming] what? I forgot to take the "math required course"!

图模型2--2022-5-13

MySQL - 唯一索引

Sentinel three flow control modes

N叉树、page_size、数据库严格模式修改、数据库中delect和drop的不同

zoj-Swordfish-2022-5-6

很佩服的一个Google大佬,离职了。。

《nlp入门+实战:第二章:pytorch的入门使用 》

Ffmpeg splash screen solution (modify the source code and discard incomplete frames)

Arduino + AD9833 波形发生器
随机推荐
563页(30万字)智慧化工园区(一期)总体设计方案
Daily three questions 7.22
MySQL - hiding and deleting indexes
脚手架内各文件配置说明、组件化开发步骤
Pytorch common tricks summary
Figure model 2-2022-5-13
MySQL - full text index
CMS vulnerability recurrence - foreground arbitrary user password modification vulnerability
zoj-Swordfish-2022-5-6
MySQL - normal index
图像处理:RGB565转RGB888
Scope usage in POM file dependency
常量指针、指针常量
谷歌联合高校研发通用模型ProteoGAN,可设计生成具有新功能的蛋白质
Qt创建应用程序托盘及相关功能
Qt应用程序防止多开,即单例运行
Simply use golang SQLC to generate MySQL query code
Zoj1137+ operation 1 -- May 28, 2022
Activity exception lifecycle
The paper of gaojingjian center was selected into the ACL 2022 of the international summit to further expand the privacy computing capacity of Chang'an chain