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



边栏推荐
- 在Pytorch中使用Tensorboard可视化训练过程
- 周大福践行「百周年承诺」,真诚服务推动绿色环保
- Change multiple file names with one click
- [C question set] of Ⅷ
- 两个BI开发,3000多张报表?如何做的到?
- mysql8.0JSON_ Instructions for using contains
- 启牛证券账户怎么开通,开户安全吗?
- CPU design practice - Chapter 4 practice task 3 use pre delivery technology to solve conflicts caused by related issues
- Is the securities account given by the head teacher of qiniu school safe? Can I open an account?
- 01. Solr7.3.1 deployment and configuration of jetty under win10 platform
猜你喜欢

有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器

leetcode:881. lifeboat

实现一个博客系统----使用模板引擎技术

IPv6与IPv4的区别 网信办等三部推进IPv6规模部署

浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数

Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发

Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management

Thymeleaf th:classappend属性追加 th:styleappend样式追加 th:data-自定义属性

CPU设计相关笔记

【NVMe2.0b 14-9】NVMe SR-IOV
随机推荐
在Pytorch中使用Tensorboard可视化训练过程
Is it OK to open the securities account on the excavation finance? Is it safe?
【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)
leetcode:881. lifeboat
04_ Use of solrj7.3 of solr7.3
be careful! Software supply chain security challenges continue to escalate
外盘入金都不是对公转吗,那怎么保障安全?
The function of qualifier in C language
[learning notes] connectivity and circuit of graph
webRTC SDP mslabel lable
Isn't it right to put money into the external market? How can we ensure safety?
最长公共子序列 - 动态规划
启牛证券账户怎么开通,开户安全吗?
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
Interview shock 62: what are the precautions for group by?
SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
Thymeleaf common functions
超级哇塞的快排,你值得学会!
The forked VM terminated without saying properly goodbye
Photoshop插件-动作相关概念-非加载执行动作文件中动作-PS插件开发

