当前位置:网站首页>[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 )
边栏推荐
- Share 20 strange JS expressions and see how many correct answers you can get
- 【招聘岗位】软件工程师(全栈)- 公共安全方向
- Strong connection component
- The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
- [summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
- 动态规划
- leetcode:881. 救生艇
- CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
- 【招聘岗位】基础设施软件开发人员
- 申请代码签名证书时如何选择合适的证书品牌?
猜你喜欢
ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
黑马程序员-软件测试-10阶段2-linux和数据库-44-57为什么学习数据库,数据库分类关系型数据库的说明Navicat操作数据的说明,Navicat操作数据库连接说明,Navicat的基本使用,
【学习笔记】阶段测试1
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 2)
微帧科技荣获全球云计算大会“云鼎奖”!
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
Thymeleaf th:with局部变量的使用
CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
随机推荐
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
Shanghai under layoffs
安装配置Jenkins
Postgresql 13 安装
3W principle [easy to understand]
Change multiple file names with one click
LeetCode_ 2 (add two numbers)
Thymeleaf 常用函數
微帧科技荣获全球云计算大会“云鼎奖”!
leetcode:881. lifeboat
Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
Fonctions communes de thymeleaf
Explain Vue's plan to clean up keepalive cache in time
Thymeleaf 模板的创建与使用
Time to calculate cron expression based on cronsequencegenerator
美国费城发生“安全事故” 2名警察遭枪杀
How can non-technical departments participate in Devops?
FR练习题目---简单题
世界环境日 | 周大福用心服务推动减碳环保
【NVMe2.0b 14-9】NVMe SR-IOV