当前位置:网站首页>[12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!
[12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!
2022-07-05 14:42:00 【Keep writing questions every day】
as everyone knows , Pointer is C The soul of language , Many people just fall at the foot of the pointer . today , Let me show you how the pointer is tested in the written exam !( Tips : The test environment of this blog is vs-x64, In this environment, the pointer occupies 8 Bytes )
Exercises 1
int main()
{
int a[4] = { 1,2,3,4 };
printf("%u\n", sizeof(a));//16-sizeof( Array name )- Entire array
printf("%u\n", sizeof(a+0));//8- Not a separate array name - Address of the first element of the array
printf("%u\n", sizeof(*a));//4- Address dereference of the first element of the array - Array head element
printf("%u\n", sizeof(a+1));//8- Array number 2 Addresses of elements
printf("%u\n", sizeof(a[1]));//4- Array number 2 Elements
printf("%u\n", sizeof(&a));//8- The address of the entire array
printf("%u\n", sizeof(*&a));//16- Dereference the address of the entire array , What you get is the whole array -sizeof( Array name )
printf("%u\n", sizeof(&a+1));//8-&a Is the address of the entire array ,+1 Skip an array , Or the address of an array
printf("%u\n", sizeof(&a[0]));//8- The address of the first element
printf("%u\n", sizeof(&a[0]+1));//8- The first 2 Addresses of elements
return 0;
}
Exercises 2
int main()
{
char str[5] = { 'a','b','c','d','e' };
printf("%d\n", sizeof(str));//5-sizeof( Array name )- Entire array
printf("%d\n", sizeof(str+0));//8- Address of the first element of the array
printf("%d\n", sizeof(*str));//1- Array head element
printf("%d\n", sizeof(str[1]));//1- The second element of the array
printf("%d\n", sizeof(&str));//8
printf("%d\n", sizeof(&str[0]+1));//8
printf("%d\n", sizeof(str[0]+1));//4- Add characters and integers - Improve the overall shape
// Equivalent to printf("%d\n", sizeof('a'+1));//4- Improve the overall shape
return 0;
}
Exercises 3
int main()
{
char str[] = { 'a','b','c','d','e' };
printf("%d\n", strlen(str));// Random value
printf("%d\n", strlen(str+0));// Random value
printf("%d\n", strlen(*str));//strlen('a')-->strlen(97);// Wild pointer
printf("%d\n", strlen(str[1]));// Wild pointer
printf("%d\n", strlen(&str));// Random value
printf("%d\n", strlen(&str+1));// Random value
printf("%d\n", strlen(&str[0]+1));// Random value
return 0;
}
The third one here printf If debugging , This error means accessing illegal memory ( There are some memory addresses that cannot be accessed , Some are allowed to access , But certain tests will also be carried out )
About sizeof and strlen:
strlen Is to find the length of the string , Focus on... In the string '\0', The calculation is \0 The number of characters that appear before
strlen It's a library function , For strings only
sizeof Only pay attention to the size of memory , Don't care what's in the memory
sizeof It's the operator
Exercises 4
int main()
{
int a[3][4] = { 0 };
printf("%d\n", sizeof(a));//3*4*4=48- The entire two-dimensional array
printf("%d\n", sizeof(a[0]));//8 wrong //4*4=16 Yes sizeof( One dimensional array array name )- The whole first one-dimensional array
printf("%d\n", sizeof(a[0]+1));//8- The first 2 The address of a one-dimensional array
printf("%d\n", sizeof(a[0][0]));//4- The... Of the first one-dimensional array 1 Elements
printf("%d\n", sizeof(*(a[0]+1)));//4- The... Of the first one-dimensional array 2 Elements
printf("%d\n", sizeof(*(a+1)));//8 wrong //4*4=16 Yes sizeof( One dimensional array array name )- The whole 2 One dimensional array
printf("%d\n", sizeof(a+1));//8- The first 2 The address of a one-dimensional array
printf("%d\n", sizeof(&a[0]+1));//8- The... Of the first one-dimensional array 2 Elements
printf("%d\n", sizeof(*a));//4*4=16- The whole first one-dimensional array
printf("%d\n", sizeof(a[3]));//16- The whole fourth one-dimensional array
return 0;
}
Exercises 5
int main()
{
int a[5] = { 1,2,3,4,5 };
int* ptr = (int*)(&a + 1);
printf("%d %d", *(a + 1), *(ptr - 1));//2 5
return 0;
}
Exercises 6
struct Test
{
int num1;
char* num2;
short num3;
char num4[2];
short num5[4];
}*p;
// hypothesis p The value of is 0x100000, What are the values of the following expressions ?
// Known in the current environment ,Test The variable size of type is 20 Bytes
// Investigate : The pointer / Integers +1 How much is added
int main()
{
printf("%p\n", p + 0x1);//0x100014
printf("%p\n", (unsigned long)p + 0x1);//0x100001
printf("%p\n", (unsigned int*)p + 0x1);//0x100004
return 0;
}
0x1 It's hexadecimal 1, That's decimal 1:
For the first p+0x1 in p yes struct Test* Pointer to type , Add 1, Skip one sizeof(struct Test)==20, That is to say 0x00014 size .
Similarly, the second one p Is forced to unsigned long type , It's an integer , Integers =1 Namely +1, And then according to %p Print out , Add because 1( Not more than 16), So and in the original 0x10000 add 1 It's the same .
Similarly, the third p Is forced to unsigned int* type , Add 1, Plus 4 Bytes .
Exercises 7
int main()
{
int a[4] = { 1,2,3,4 };
int* ptr1 = (int*)(&a + 1);
int* ptr2 = (int*)(a + 1);
int* ptr3 = (int*)((int)a + 1);
int* ptr4 = (int*)((char*)a + 1);
printf("%x %x %x %x\n\n", ptr1[-1], *ptr2,*ptr3,*ptr4);//
return 0;
}
ptr3 The way to get it is :
By casting an integer pointer to an integer value , Then add 1, Finally, it is cast to an integer pointer ;
ptr4 The way to get it is :
By converting an integer pointer to a character pointer , Then add 1, Finally, it is cast to an integer pointer .
The effect of the two is the same .
Exercises 8
int main()
{
int a[3][2] = { (0,1),(2,3),(3,4) };// Pit point : Comma expression
int* p;
p = a[0];
printf("%d\n", p[0]);
// Equivalent to printf("%d\n", *p);
return 0;
}
Exercises 9
int main()
{
int a[4][5];
int(*p)[3];// The point is here 3, No 5
p = a;
printf("%p %d\n", &p[3][2] - &a[3][2], &p[3][2] - &a[3][2]);
return 0;
}
About -6 change FFFFFA:
practice 10
int main()
{
int a[2][5] = { 1,2,3,4,5,6,7,8,9,10 };
int* ptr1 = (int*)(&a + 1);
int* ptr2 = (*(a + 1));
// Equivalent to int* ptr2 = (int*)(a + 1);
printf("%d %d\n", *(ptr1 - 1), *(ptr2 - 1));//10 5
return 0;
}
practice 11
int main()
{
char* a[] = { "I will work","at","alibaba" };
char** pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}
Exercises 12
int main()
{
char* c[] = { "ENTRE","NEW","POINT","FIRST" };
char** cp[] = { c + 3,c + 2,c + 1,c };
char*** cpp = cp;
printf("%s\n", **++cpp);
printf("%s\n", *--*++cpp+3);
printf("%s\n", *cpp[-2]+3);
printf("%s\n", cpp[-1][-1]+1);
return 0;
}
Here is my own way of understanding :
[] and * You can cross the bridge ( That is to find the target pointed to by the pointer ), cpp[-1] That's what it points to *(cpp-1), It's the pointer first -1( The direction has changed ), And then across the bridge ( Find the target pointed to by the pointer );
Then the dotted line here is that there will be no self increase ++ Have side effects
a=a+1; Have side effects (a Changed )
however a+1; No side effects (a No change )
边栏推荐
- Fonctions communes de thymeleaf
- 两个BI开发,3000多张报表?如何做的到?
- Countermeasures of enterprise supply chain management system in UCA Era
- Type declaration of all DOM elements in TS
- Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
- useMemo,memo,useRef等相关hooks详解
- [detailed explanation of Huawei machine test] character statistics and rearrangement
- MongDB学习笔记
- 最长公共子序列 - 动态规划
- Explain Vue's plan to clean up keepalive cache in time
猜你喜欢
基于TI DRV10970驱动直流无刷电机
CPU design related notes
安装配置Jenkins
LeetCode_ 2 (add two numbers)
Countermeasures of enterprise supply chain management system in UCA Era
Thymeleaf th:with use of local variables
[detailed explanation of Huawei machine test] character statistics and rearrangement
两个BI开发,3000多张报表?如何做的到?
实现一个博客系统----使用模板引擎技术
Topology visual drawing engine
随机推荐
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
Is the securities account given by the head teacher of qiniu school safe? Can I open an account?
LeetCode_ 3 (longest substring without repeated characters)
LeetCode_ 69 (square root of x)
直播预告|如何借助自动化工具落地DevOps(文末福利)
Sharing the 12 most commonly used regular expressions can solve most of your problems
Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
两个BI开发,3000多张报表?如何做的到?
如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴
The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
Handwriting promise and async await
开挖财上的证券账户可以吗?安全吗?
动态规划
FR练习题目---综合题
Principle and performance analysis of lepton lossless compression
最长公共子序列 - 动态规划
做自媒體視頻二次剪輯,怎樣剪輯不算侵權
CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
黑马程序员-软件测试-10阶段2-linux和数据库-44-57为什么学习数据库,数据库分类关系型数据库的说明Navicat操作数据的说明,Navicat操作数据库连接说明,Navicat的基本使用,