当前位置:网站首页>[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 )
边栏推荐
- Source insight project import and use tutorial
- Crawl IP
- MySQL command statement (personal summary)
- Implementation of memmove in C language
- Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency
- Servlet learning notes
- How to write the SQL statement of time to date?
- Know small and medium LAN WLAN
- English translation Arabic - batch English translation Arabic tools free of charge
- [C language] simulation implementation of pow function (recursion)
猜你喜欢

XOR operation and its usage

Basic knowledge of communication network 01

Theoretical knowledge of digital image (I) (personal analysis)

Prometheus deployment

Sprint for golden nine and silver ten, stay up at night for half a month, collect 1600 real interview questions from Android post of major manufacturers

Advanced notes (Part 2)

Deploy ZABBIX automatically with saltstack

How navicate modifies the database name

What is the process of swing event processing?

Implementation of strstr in C language
随机推荐
XOR operation and its usage
Tencent cloud deployment lamp_ Experience of building a station
MySQL8 Encrypting InnoDB Tablespaces
Intermediate soft test (system integration project management engineer) high frequency test site
CodeIgnier框架实现restful API接口编程
leetcode day3 超过经理收入的员工
软考中级(系统集成项目管理工程师)高频考点
并发程序设计,你真的懂吗?
Sprint for golden nine and silver ten, stay up at night for half a month, collect 1600 real interview questions from Android post of major manufacturers
毕马威中国:证券基金经营机构信息技术审计项目发现洞察
Function fitting based on MATLAB
Const pointer of C language and parameter passing of main function
How to write the SQL statement of time to date?
C language function
[C language] summary of methods for solving the greatest common divisor
Redis笔记
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
Failed to install app-debug. apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
Read how to deploy highly available k3s with external database
Question bank and answers of the latest national fire-fighting facility operators (intermediate fire-fighting facility operators) in 2022