当前位置:网站首页>[advanced C language] 8 written questions of pointer
[advanced C language] 8 written questions of pointer
2022-07-07 01:25:00 【Muxixi】
I'm Muxi Xi
List of articles
Written test questions of pointer
The meaning of array names :
- sizeof( Array name ), The array name here represents the entire array , It calculates the size of the entire array .
- & Array name , The array name here represents the entire array , It takes out the address of the entire array .
- In addition, all array names represent the address of the first element .
Written test question 1
#include<stdio.h>
int main()
{
int a[5] = {
1, 2, 3, 4, 5 };
int* ptr = (int*)(&a + 1);
printf("%d,%d", *(a + 1), *(ptr - 1));
return 0;
}
1.&a It takes out the address of the entire array ,&a+1 Is to skip a type of int(*)[5] Array of .
2.a Not alone sizeof Inside , And the array name a There is no address symbol in front , here a Represents the address of the first element .a+1 Skip a type of int The integer of , That is, the address of the second element .
3.ptr-1 Is to skip a type of int The integer of .
4.*(a+1)–>a[1];*(ptr-1)–>ptr[-1].
Question 2 of the written examination
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}*p = (struct Test*)0x100000;
// hypothesis p The value of is 0x100000. What are the values of the expressions in the following table ?
// It is known that , Structure Test The variable size of type is 20 Bytes
//86 In the environment
int main()
{
printf("%p\n", p + 0x1);//0x100000+20-->0x100014
//p+1 Indicates skipping a structure , The size of a structure is 20 Bytes
printf("%p\n", (unsigned long)p + 0x1);
//0x100000+1-->0x100001
printf("%p\n", (unsigned int*)p + 0x1);//0x100000+4-->0x100004
p+1 Indicates skipping a type of unsigned int The integer of , Four bytes
return 0;
}
Written test question 3
#include<stdio.h>
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;
}
1.ptr[-1] amount to *(ptr-1)
2.&a It takes out the address of the entire array ,&a+1 Is to skip a type of int(*)[4] Array of .
3.a Not alone sizeof Inside , And the array name a There is no address symbol in front , here a Represents the address of the first element .a Forced type conversion to int,a+1 Add integers , Then forcibly convert it into a pointer , Skipped a byte , One int The type size is 4 Bytes , The data is stored in the small end , Therefore, by pointing 01 The pointer to , Yes 00. and prt2 The access right of is to access 4 Bytes , so ptr2 What I visited was 0x02000000.
Written test question 4
#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;
}
because int a[3][2] = {(0,1),(2,3),(4,5)} It's a comma expression .
Comma expression , Is multiple expressions separated by commas .
Comma expression , From left to right . The result of the entire expression is the result of the last expression .
so int a[3][2] ={1,3,5};
a[0] Is the array name in the first row ,a[0] Represents the address of the first element , namely a[0][0] The address of ,&a[0][0].
Written test question five
#include<stdio.h>
int main()
{
int a[5][5];
int(*p)[4];
p = a;
printf("%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);
return 0;
}
1.p[4][2] amount to *(*(p+4)+2).
2. The two hands subtract , The result is the number of elements between two pointers .
3.%p What is printed directly is the complement , Addresses are considered unsigned integers .
Written test question six
#include<stdio.h>
int main()
{
int aa[2][5] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int* ptr1 = (int*)(&aa + 1);
int* ptr2 = (int*)(*(aa + 1));
printf("%d,%d", *(ptr1 - 1), *(ptr2 - 1));
return 0;
}
1.*(aa+1) amount to aa[1], Represents the array name of the second row .
2.&aa It takes out the address of the entire array ,&aa+1 Is to skip a type of int(*)[2][5] Array of .
3.aa Not alone sizeof Inside , And the array name aa There is no address symbol in front , here aa Represents the array name of the first row ,aa+1 Represents the array name of the second row .
Written test question 7
#include <stdio.h>
int main()
{
char* a[] = {
"work","at","alibaba" };
char** pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}
pa++ Is to skip a type of char* The pointer to .
Written test question 8
#include<stdio.h>
int main()
{
char* c[] = {
"ENTER","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;
}
1.*cpp[-2] amount to *(*(cpp-2)).
2.cpp[-1][-1] amount to *(*(cpp-1)-1)
printf(“%s\n”, **++cpp); The result is :
printf(“%s\n”, *-- * ++cpp + 3); The result is :
printf(“%s\n”, *cpp[-2] + 3); The result is :
printf(“%s\n”, cpp[-1][-1] + 1); The result is :
At the end
Then the notes of pointer pen test questions are over here , What doubts or feelings are wrong , Leave a comment in the comments section .
But the road ahead , Not to lose time !
边栏推荐
- [100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
- Do you understand this patch of the interface control devaxpress WinForms skin editor?
- Neon Optimization: an optimization case of log10 function
- c语言—数组
- Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
- Boot - Prometheus push gateway use
- 405 method not allowed appears when the third party jumps to the website
- 字节P7专业级讲解:接口测试常用工具及测试方法,福利文
- Installation of gazebo & connection with ROS
- SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
猜你喜欢
Transformation transformation operator
boot - prometheus-push gateway 使用
力扣1037. 有效的回旋镖
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
LLDP兼容CDP功能配置
云呐|工单管理软件,工单管理软件APP
[Niuke] [noip2015] jumping stone
Can the system hibernation file be deleted? How to delete the system hibernation file
c语言—数组
Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe
随机推荐
Segmenttree
Failed to successfully launch or connect to a child MSBuild. exe process. Verify that the MSBuild. exe
Make Jar, Not War
【案例分享】网络环路检测基本功能配置
[JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
NEON优化:性能优化经验总结
The MySQL database in Alibaba cloud was attacked, and finally the data was found
Installation and testing of pyflink
移植DAC芯片MCP4725驱动到NUC980
NEON优化:log10函数的优化案例
Supersocket 1.6 creates a simple socket server with message length in the header
Installation of gazebo & connection with ROS
Taro 小程序开启wxml代码压缩
Transformation transformation operator
「笔记」折半搜索(Meet in the Middle)
Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe
分享一个通用的so动态库的编译方法
斗地主游戏的案例开发
Boot - Prometheus push gateway use
Force buckle 1037 Effective boomerang