当前位置:网站首页>[C language] advanced pointer exercise 1
[C language] advanced pointer exercise 1
2022-07-28 20:04:00 【An ran_】
List of articles
One 、 Pointer advanced key knowledge summary
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-kKStJksX-1657275966100)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220702095018660.png)]](/img/71/8dcdd447febae8e024cc7ab66e51ae.png)
Two 、 Advanced pointer exercises 1
Please think about the running results of the following programs , Give the right answer .
// One dimensional array
int a[] = {
1,2,3,4};
printf("%d\n",sizeof(a));
printf("%d\n",sizeof(a+0));
printf("%d\n",sizeof(*a));
printf("%d\n",sizeof(a+1));
printf("%d\n",sizeof(a[1]));
printf("%d\n",sizeof(&a));
printf("%d\n",sizeof(*&a));
printf("%d\n",sizeof(&a+1));
printf("%d\n",sizeof(&a[0]));
printf("%d\n",sizeof(&a[0]+1));
// A character array
char arr[] = {
'a','b','c','d','e','f'};
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(arr+0));
printf("%d\n", sizeof(*arr));
printf("%d\n", sizeof(arr[1]));
printf("%d\n", sizeof(&arr));
printf("%d\n", sizeof(&arr+1));
printf("%d\n", sizeof(&arr[0]+1));
printf("%d\n", strlen(arr));
printf("%d\n", strlen(arr+0));
printf("%d\n", strlen(*arr));
printf("%d\n", strlen(arr[1]));
printf("%d\n", strlen(&arr));
printf("%d\n", strlen(&arr+1));
printf("%d\n", strlen(&arr[0]+1));
char arr[] = "abcdef";
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(arr+0));
printf("%d\n", sizeof(*arr));
printf("%d\n", sizeof(arr[1]));
printf("%d\n", sizeof(&arr));
printf("%d\n", sizeof(&arr+1));
printf("%d\n", sizeof(&arr[0]+1));
printf("%d\n", strlen(arr));
printf("%d\n", strlen(arr+0));
printf("%d\n", strlen(*arr));
printf("%d\n", strlen(arr[1]));
printf("%d\n", strlen(&arr));
printf("%d\n", strlen(&arr+1));
printf("%d\n", strlen(&arr[0]+1));
char *p = "abcdef";
printf("%d\n", sizeof(p));
printf("%d\n", sizeof(p+1));
printf("%d\n", sizeof(*p));
printf("%d\n", sizeof(p[0]));
printf("%d\n", sizeof(&p));
printf("%d\n", sizeof(&p+1));
printf("%d\n", sizeof(&p[0]+1));
printf("%d\n", strlen(p));
printf("%d\n", strlen(p+1));
printf("%d\n", strlen(*p));
printf("%d\n", strlen(p[0]));
printf("%d\n", strlen(&p));
printf("%d\n", strlen(&p+1));
printf("%d\n", strlen(&p[0]+1));
// Two dimensional array
int a[3][4] = {
0};
printf("%d\n",sizeof(a));
printf("%d\n",sizeof(a[0][0]));
printf("%d\n",sizeof(a[0]));
printf("%d\n",sizeof(a[0]+1));
printf("%d\n",sizeof(*(a[0]+1)));
printf("%d\n",sizeof(a+1));
printf("%d\n",sizeof(*(a+1)));
printf("%d\n",sizeof(&a[0]+1));
printf("%d\n",sizeof(*(&a[0]+1)));
printf("%d\n",sizeof(*a));
printf("%d\n",sizeof(a[3]));
3、 ... and 、 Advanced pointer exercises 1 Answer and analysis
explain , So here's what I chose x86 Environment is 32 Bit platform , So the results of pointer size calculation are 4. But I didn't give the premise when analyzing , So two results are given 4/8.
( One ) answer
1. The first group
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-jjtdaR0s-1657275966102)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220702120550698.png)]](/img/ee/c62919330edb4a0b5a2a4b027e5b5c.png)
2. The second group
(1) The first group
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-HHHAbQcA-1657275966104)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220702102107357.png)] It's obvious from the above picture that it's hanging [ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-QqSgk6VD-1657275966105)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220702102311629.png)]](/img/23/37009eb3b0fafe3bd417bbeedfa64e.png)
(2) The second group 
It's obvious from the above picture that it's hanging 
(3) The third group ![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-nuwvDOkd-1657275966107)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220702104304030.png)]](/img/cb/6eda68001d2cd924639060d7ad4d20.png)
It's obvious from the above picture that it's hanging
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-GpLXvs38-1657275966108)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220702104356794.png)]](/img/1e/12dcace76f72e25f5b3f80a12a7ada.png)
3. The third group
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-meUqkJCY-1657275966109)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220702112517916.png)]](/img/84/6e47ba6ece1940d24cec11926d19e1.png)
( Two ) analysis
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-NGxcXYV7-1657275966109)(C:\Users\19271\Pictures\ Drawing analysis \ The first step \ Advanced pointer exercises .png)]](/img/24/eab662860cbde238ee775c56c7f5bb.png)
Four 、 Focus on
1.sizeof Only focus on type, not content and meaning ;strlen Pay attention to the end of string \0.
2. Array names have only two meanings , Most of the time, it represents the address of the first element of the array , There are only two cases that represent the entire array ——sizeof( Array name )【 Calculate the memory size of the entire array 】,& Array name 【 The address of the entire array 】
3. The result of pointer operation has a lot to do with the meaning of pointer type . Pointer types have two meanings , One is pointer execution +1/-1 How far is the address skipped by the operation of step , Another is the permission of pointer dereference , for example int The dereference permission of type is 4 Bytes ,char Yes. 1 Bytes ,double* Yes. 8 Bytes, etc .( We can adopt different types according to our own needs )
边栏推荐
- [in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
- Application skills of programming rising and falling edge instructions of botu 1200/1500plc (bool array)
- Advanced notes (Part 2)
- C language array and bubble sort
- leetcode day4 部门工资最高的员工
- [experience] some suggestions and experience on repairing electronic equipment
- MySQL command statement (personal summary)
- What is the process of swing event processing?
- MySQL performance testing tool sysbench learning
- Netcoreapi operation excel table
猜你喜欢

毕马威中国:证券基金经营机构信息技术审计项目发现洞察

C language pointer and two-dimensional array

Servlet learning notes

CodeIgnier框架实现restful API接口编程

Basic usage of docker

通信网络基础知识01

云计算笔记part.2——应用管理

Thoroughly understand bit operations -- and (&), not (~), or (|), XOR (^)

Rand function generates pseudo-random numbers

Tencent cloud deployment lamp_ Experience of building a station
随机推荐
Article translation software - batch free translation software supports major translation interfaces
A chip company fell in round B
C language pointer and two-dimensional array
时间转日期的sql语句应该怎么写?
English translation Italian - batch English translation Italian tools free of charge
[网络]跨区域网络的通信学习IPv4地址的分类和计算
数字滤波器设计——Matlab
There are five certificates in the soft examination advanced examination, which is more worth taking?
leetcode day3 查找重复的电子邮箱
Idea properties file display \u solution of not displaying Chinese
Deploy ZABBIX automatically with saltstack
KPMG China: insights into information technology audit projects of securities fund management institutions
Leetcode day4 the highest paid employee in the Department
云计算笔记part.2——应用管理
Intermediate soft test (system integration project management engineer) high frequency test site
Serial port receiving application ring buffer
C language array and bubble sort
Special draft of Mir | common sense knowledge and reasoning: representation, acquisition and application (deadline on October 31)
C language implementation of strncpy
Implementation of memcpy in C language