当前位置:网站首页>[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
边栏推荐
- Scope usage in POM file dependency
- 分布式事务处理方案大 PK!
- PostgreSQL rounding
- 563页(30万字)智慧化工园区(一期)总体设计方案
- Onpropertychange event "suggestions collection"
- NIO知识点
- [sword finger offer II 115. reconstruction sequence]
- Chapter V Modification implementation (impl) class
- PC Museum (2) 1972 hp-9830a
- Is it safe to open an online stock account?
猜你喜欢

Arduino + AD9833 波形发生器

Array element removal problem

ECCV 2022 | 清华提出首个嵌入光谱稀疏性的Transformer

Scan line, weight segment tree

MySQL - 普通索引

PC博物馆(1) 1970年 Datapoint 2000

MySQL - 唯一索引

OSPF includes special area experiments, mGRE construction and re release

Websocket 协议解读-RFC6455

ZOJ 2770 differential restraint system -- 2 -- May 20, 2022
随机推荐
很佩服的一个Google大佬,离职了。。
Machine learning quiz (11) verification code recognition test - deep learning experiment using QT and tensorflow2
Simply use golang SQLC to generate MySQL query code
Zoj1137+ operation 1 -- May 28, 2022
Qt创建应用程序托盘及相关功能
差分约束系统---1且2--2022年5月27日
NLP introduction + practice: Chapter 2: introduction to pytorch
Sentinel flow control quick start
脚手架文件目录说明、文件暴露
Mina framework introduction "suggestions collection"
Sentinel three flow control effects
Binlog and iptables prevent nmap scanning, xtrabackup full + incremental backup, and the relationship between redlog and binlog
每日三题 7.21
实时天气API
Problem solving -- question 283 of leetcode question bank
MySQL - 多列索引
What is NFT? How to develop NFT system?
Android uses JDBC to connect to a remote database
Interpretation of websocket protocol -rfc6455
5个最佳WordPress广告插件