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



边栏推荐
- Time to calculate cron expression based on cronsequencegenerator
- [detailed explanation of Huawei machine test] character statistics and rearrangement
- anaconda使用中科大源
- STM32+BH1750光敏传感器获取光照强度
- Longest common subsequence dynamic programming
- 美国费城发生“安全事故” 2名警察遭枪杀
- 【NVMe2.0b 14-9】NVMe SR-IOV
- FR练习题目---综合题
- Structure - C language
- Fonctions communes de thymeleaf
猜你喜欢

CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕

Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading

Principle and performance analysis of lepton lossless compression

【华为机试真题详解】字符统计及重排

Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment

leetcode:881. 救生艇

LeetCode_ 2 (add two numbers)

一键更改多个文件名字

leetcode:881. lifeboat

Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
随机推荐
动态规划
Run faster with go: use golang to serve machine learning
Loop invariant
Using tensorboard to visualize the training process in pytoch
【招聘岗位】基础设施软件开发人员
浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数
两个BI开发,3000多张报表?如何做的到?
PMP考试20天能通过吗?
Pointer operation - C language
危机重重下的企业发展,数字化转型到底是不是企业未来救星
Postgresql 13 安装
Is the securities account given by the head teacher of qiniu school safe? Can I open an account?
Principle and performance analysis of lepton lossless compression
Implement a blog system -- using template engine technology
黑马程序员-软件测试-10阶段2-linux和数据库-44-57为什么学习数据库,数据库分类关系型数据库的说明Navicat操作数据的说明,Navicat操作数据库连接说明,Navicat的基本使用,
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
03_ Dataimport of Solr
webRTC SDP mslabel lable
超级哇塞的快排,你值得学会!
【华为机试真题详解】字符统计及重排

