当前位置:网站首页>[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
边栏推荐
- [sword finger offer II 115. reconstruction sequence]
- Sentinel three flow control modes
- Sentinel three flow control effects
- Add a love power logo to your website
- [recommendation system] the classic technical architecture of the data flow of the recommendation system + the most complete evolution map of the top 10 deep learning CTR models of Microsoft, Alibaba,
- Interpretation of websocket protocol -rfc6455
- IEPE vibration sensor synchronous signal acquisition card /icp synchronous data network acquisition module
- [carving master learning programming] Arduino hands-on (59) - RS232 to TTL serial port module
- NIO知识点
- NiO knowledge points
猜你喜欢

关联规则--2022年7月10日

MySQL - 索引的隐藏和删除

Figure model 2-2022-5-13
![[electronic device note 4] inductance parameters and type selection](/img/b1/6c5cb27903bc1093bf49e278e31353.png)
[electronic device note 4] inductance parameters and type selection

String__

机器学习小试(11)验证码识别测试-使用Qt与Tensorflow2进行深度学习实验

MySQL - 多列索引

Analysis of distributed lock redistribution principle

Arduino + AD9833 波形发生器

ZOJ 2770 differential restraint system -- 2 -- May 20, 2022
随机推荐
N叉树、page_size、数据库严格模式修改、数据库中delect和drop的不同
Is it safe to open an online stock account?
Add a love power logo to your website
Erlang学习02
[sword finger offer II 115. reconstruction sequence]
Chapter V Modification implementation (impl) class
pom文件dependency中的 scope用法
[recommendation system] the classic technical architecture of the data flow of the recommendation system + the most complete evolution map of the top 10 deep learning CTR models of Microsoft, Alibaba,
Sentinel implements the persistence of pull pattern rules
Arduino + AD9833 waveform generator
MySQL - 更新表中的数据记录
MySQL - 多列索引
Activity exception lifecycle
MySQL - lock
Machine learning quiz (11) verification code recognition test - deep learning experiment using QT and tensorflow2
ECCV 2022 | 清华提出首个嵌入光谱稀疏性的Transformer
Protocol Bible - talk about ports and quads
NIO知识点
Erlang学习番外
Daily three questions 7.21