当前位置:网站首页>Detailed explanation of pointer written test questions (C language)
Detailed explanation of pointer written test questions (C language)
2022-07-28 03:52:00 【Brother Xiao who is not bald】
Directory copy
List of articles
Preface
Last time we had a deep understanding of the usage of pointers , This time we will understand the written test of pointer , To deepen the understanding of pointers , Don't talk nonsense , Let's get started. ~
One 、 Pointer and array written test questions
About this part of the topic , We need to know something first :
1. sizeof( Array name ), The array name here represents the entire array, and the byte size of the entire array is calculated ;
2. & Array name , The array name here represents the whole array , It takes out the address of the entire array ;
3. After removing the above two cases , The array name represents the address of the first element of the array .
1. One dimensional array
int a[] = {
1,2,3,4};
printf("%d\n",sizeof(a));//1.
printf("%d\n",sizeof(a+0));//2.
printf("%d\n",sizeof(*a));//3.
printf("%d\n",sizeof(a+1));//4.
printf("%d\n",sizeof(a[1]));//5.
printf("%d\n",sizeof(&a));//6.
printf("%d\n",sizeof(*&a));//7.
printf("%d\n",sizeof(&a+1));//8.
printf("%d\n",sizeof(&a[0]));//9.
printf("%d\n",sizeof(&a[0]+1));//10.
answer :
- 4*4 = 16;
- because a+0 No longer sizeof Add the group name , At this time a+0 Is the address of the first element of the array , So what is printed is the byte size of the address , stay 32 The size of the bit operating system is 4 byte , stay 64 The size of the bit operating system is 8 byte ;( In the following topic , If you calculate pointer size , I am abbreviated as 4/8)
- 4;
- 4/8;
- 4;
- &a Is the address of the entire array , It's the address , So the size is 4/8;
- *&a Equivalent to a, So we calculate the size of the whole array , by 16;
- 4/8;
- 4/8;
- 4/8
2. A character array
2.1
char arr[] = {
'a','b','c','d','e','f'};
printf("%d\n", sizeof(arr));//1
printf("%d\n", sizeof(arr+0));//2
printf("%d\n", sizeof(*arr));//3
printf("%d\n", sizeof(arr[1]));//4
printf("%d\n", sizeof(&arr));//5
printf("%d\n", sizeof(&arr+1));//6
printf("%d\n", sizeof(&arr[0]+1));//7
printf("%d\n", strlen(arr));//8
printf("%d\n", strlen(arr+0));//9
printf("%d\n", strlen(*arr));//10
printf("%d\n", strlen(arr[1]));//11
printf("%d\n", strlen(&arr));//12
printf("%d\n", strlen(&arr+1));//13
printf("%d\n", strlen(&arr[0]+1));//14
- 1*6 = 6;
- 4/8;
- 1;
- 1;
- 4/8;
- 4/8;
- 4/8
- because strlen Is to calculate the incoming character array ‘\0’ The number of characters before , and arr There is no ‘\0’, So the calculated value is random ;
- Random value ;
- because strlen To pass a pointer , but *arr It's not a pointer , So there's an error ;
- Report errors ;
- although &arr What you get is the address of the whole array , But the value and arr It's the same , It's just different types , Pass in strlen After that, the type will be changed to char*, So what we get is random value ;
- Empathy , The result is still a random value , But different from the previous random values ;
- Random value , Different from the previous random value ;
2.2
char arr[] = "abcdef";
printf("%d\n", sizeof(arr));//1
printf("%d\n", sizeof(arr+0));//2
printf("%d\n", sizeof(*arr));//3
printf("%d\n", sizeof(arr[1]));//4
printf("%d\n", sizeof(&arr));//5
printf("%d\n", sizeof(&arr+1));//6
printf("%d\n", sizeof(&arr[0]+1));//7
printf("%d\n", strlen(arr));//8
printf("%d\n", strlen(arr+0));//9
printf("%d\n", strlen(*arr));//10
printf("%d\n", strlen(arr[1]));//11
printf("%d\n", strlen(&arr));//12
printf("%d\n", strlen(&arr+1));//13
printf("%d\n", strlen(&arr[0]+1));//14
- Because the end of the string will follow ’\0’, So the array size is 7
- 4/8
- 1
- 1
- 4/8
- 4/8
- 4/8
- 6
- 6
- because strlen To pass a pointer , but *arr It's not a pointer , So there's an error
- Report errors
- although &arr What you get is the address of the whole array , But the value and arr It's the same , It's just different types , Pass in strlen After that, the type will be changed to char*, So what comes out is 6;
- Random value
- 5
2.3
char *p = "abcdef";
printf("%d\n", sizeof(p));//1
printf("%d\n", sizeof(p+1));//2
printf("%d\n", sizeof(*p));//3
printf("%d\n", sizeof(p[0]));//4
printf("%d\n", sizeof(&p));//5
printf("%d\n", sizeof(&p+1));//6
printf("%d\n", sizeof(&p[0]+1));//7
printf("%d\n", strlen(p));//8
printf("%d\n", strlen(p+1));//9
printf("%d\n", strlen(*p));//10
printf("%d\n", strlen(p[0]));//11
printf("%d\n", strlen(&p));//12
printf("%d\n", strlen(&p+1));//13
printf("%d\n", strlen(&p[0]+1));//14
The problem is to create a character pointer p Points to the first character of the constant string , It is no longer an array created directly
- 4/8
- 4/8
- 1
- 1
- 4/8
- 4/8
- 4/8
- 6
- 5
- Report errors
- Report errors
- 6
- Random value
- 5
3. Two dimensional array
int a[3][4] = {
0};
printf("%d\n",sizeof(a));//1
printf("%d\n",sizeof(a[0][0]));//2
printf("%d\n",sizeof(a[0]));//3
printf("%d\n",sizeof(a[0]+1));//4
printf("%d\n",sizeof(*(a[0]+1)));//5
printf("%d\n",sizeof(a+1));//6
printf("%d\n",sizeof(*(a+1)));//7
printf("%d\n",sizeof(&a[0]+1));//8
printf("%d\n",sizeof(*(&a[0]+1)));//9
printf("%d\n",sizeof(*a));//10
printf("%d\n",sizeof(a[3]));//11
For two dimensional arrays ,a Is the array name of a two-dimensional array ,a[0] Is the array name in the first row of this two-dimensional array , The rest can be compared with one-dimensional arrays
- 3*4*4=48
- 4
- because a[0] Is the array name in the first row , So what we get is the size of the first row , by 4*4=16
- 4/8
- 4
- because a+1 It's not an array name , here a Is the address of the first element of the two-dimensional array ,4/8
- *(a+1) amount to a[1], So for 16
- 4/8
- &a[0] Take out the whole array of the first row of the two-dimensional array ,+1 Then skip the whole first line , For the whole second line , Dereference and get the array name in the second line , So what we get is the size of the whole second row , by 16
- 16
- 16
Two 、 Pointer written test questions ( premium )
1.
int main()
{
int a[5] = {
1, 2, 3, 4, 5 };
int *ptr = (int *)(&a + 1);
printf( "%d,%d", *(a + 1), *(ptr - 1));
return 0;
}
The result of printing is :2,5
int *ptr = (int *)(&a + 1) :&a Take out the whole array ,+1 Skip the entire array , And cast this address to int* Assign to ptr, here ptr-1 Just step back for a plastic surgery , Dereference and you get 5
2.
int main()
{
int a[4] = {
1, 2, 3, 4 };
int *ptr1 = (int *)(&a + 1);
int *ptr2 = (int *)((int)a + 1);
printf( "%x,%x", ptr1[-1], *ptr2);
return 0;
}
stay 32 The answer on the bit system is :4,2000000
a This is the first element address ,+1 Skip one int Data of type ( Numerically equivalent to the address +4), But now cast the type to int type ,+1 Is to give the address on the value +1, Convert to int* After the type ,ptr2 It points to an array a The space of the last three bytes of the first data and the space of the first byte of the second data , Dereference and you get 2000000
And in the 64 Bit operating system , The size of the pointer type is 8 byte , and int Type only 4 byte , So it's impossible to convert
3.
#include <stdio.h>
int main()
{
int a[3][2] = {
(0, 1), (2, 3), (4, 5) };
int *p;
p = a[0];
printf( "%d", p[0]);
return 0;
}
There is a hole in this question , That's the comma expression , So the number existing in the two-dimensional array is only 1,3,5
The answer is 1
4.
#include <stdio.h>
int main()
{
char *a[] = {
"work","at","alibaba"};
char**pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}

Give a matrix “at”
5.
int main()
{
char *c[] = {
"ENTER","NEW","POINT","FIRST"};
char**cp[] = {
c+3,c+2,c+1,c};
char***cpp = cp;
printf("%s\n", **++cpp);//1
printf("%s\n", *--*++cpp+3);//2
printf("%s\n", *cpp[-2]+3);//3
printf("%s\n", cpp[-1][-1]+1);//4
return 0;
}
- ++cpp It changed cpp The direction of

The answer for POINT - In front of ++ And front end – Have changed the data in the array

Give a matrix :ER - The answer for ST
- The answer for EW
边栏推荐
- 递归和非递归分别实现求第n个斐波那契数
- Interview essential skills: SQL query special training!
- Common interface testing tools
- Leetcode 0140. word splitting II
- leetcode刷题:动态规划09(最后一块石头的重量 II)
- Use of custom annotations
- R notes mice
- 动态规划——63. 不同路径 II
- Xctf attack and defense world web master advanced area php2
- [force deduction] 1337. Row K with the weakest combat effectiveness in the matrix
猜你喜欢

BRD,MRD,PRD的区别

Selenium -- Web automated testing tool

An article grasps the calculation and processing of date data in PostgreSQL

Lightpicture - exquisite drawing bed system

简单、好用的性能测试工具推荐

LabVIEW loads and uses custom symbols in tree control projects

Data mining-02

ES6 from getting started to mastering 08: extended object functions

The latest version of pagoda installs the zip extension, and PHP -m does not display the processing method

高等数学(第七版)同济大学 习题3-6 个人解答
随机推荐
LeetCode 0141. 环形链表 - 三种方法解决
Server memory failure prediction can actually do this!
Implementation of online rental system based on SSM
Summary of static blog building tools
[openvx] VX for basic use of objects_ image
Greed - 55. Jumping game
基于SSM实现在线租房系统
MySQL Basics (create, manage, add, delete, and modify tables)
Prefix-Tuning: Optimizing Continuous Prompts for Generation
Common interface testing tools
Dynamic programming - 416. Segmentation and subsets
《剑指offer》| 刷题小记
Super easy to use PC end long screenshot tool
Notes on writing questions in sword finger offer
动态规划——416. 分割等和子集
Use of custom annotations
Greedy - 53. Maximum subarray sum
input 上传文件并回显 FileReader并限制选择文件时的类型
Selenium--WEB自动化测试工具
LeetCode 0140. 单词拆分 II