当前位置:网站首页>[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 )



边栏推荐
- LeetCode_ 67 (binary sum)
- Interview shock 62: what are the precautions for group by?
- How to open an account of qiniu securities? Is it safe to open an account?
- Countermeasures of enterprise supply chain management system in UCA Era
- 【NVMe2.0b 14-9】NVMe SR-IOV
- Principle and performance analysis of lepton lossless compression
- 2022年国内正规的期货公司平台有哪些啊?方正中期怎么样?安全可靠吗?
- Webrtc learning (II)
- Topology可视化绘图引擎
- 家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
猜你喜欢

选择排序和冒泡排序

729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」

Change multiple file names with one click

世界环境日 | 周大福用心服务推动减碳环保

SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain

Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection

Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!

LeetCode_ 2 (add two numbers)

黑马程序员-软件测试-10阶段2-linux和数据库-44-57为什么学习数据库,数据库分类关系型数据库的说明Navicat操作数据的说明,Navicat操作数据库连接说明,Navicat的基本使用,

家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
随机推荐
Topology visual drawing engine
基于TI DRV10970驱动直流无刷电机
Using tensorboard to visualize the training process in pytoch
How to choose the appropriate certificate brand when applying for code signing certificate?
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
手写promise与async await
webRTC SDP mslabel lable
Matrix chain multiplication dynamic programming example
CPU design practice - Chapter 4 practical task 2 using blocking technology to solve conflicts caused by related problems
【华为机试真题详解】欢乐的周末
The function of qualifier in C language
CPU design related notes
Topology可视化绘图引擎
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
Postgresql 13 安装
Change multiple file names with one click
Longest common subsequence dynamic programming
LeetCode_ 3 (longest substring without repeated characters)
Implement a blog system -- using template engine technology
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "

