当前位置:网站首页>C pointer (interview classic topic exercise)
C pointer (interview classic topic exercise)
2022-06-10 19:51:00 【Fate friend I】
List of articles
The first question is
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[5] = {
1,2,3,4,5};
int* ptr = (int*)(&a + 1);//&a Get the address of the entire array ,&a+1 Skip the entire array
// Array pointer strongly typed to integer pointer
printf("%d,%d\n",*(a+1),*(ptr-1));// The addition and subtraction of the pointer depends on the pointer type ptr Is an integer pointer , therefore ptr-1 Point to the last of the array
return 0;
}

The second question is
#include<stdio.h>
#include<stdlib.h>
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}* p;
// hypothesis P The value of is 0x100000
// Known structures test The variable size of type is 20 Bytes
int main()
{
p = (struct Test*)0x100000;
printf("%p\n",p+0x1);//00100014 p Is a structure pointer ,p+1 Skip the entire structure
//00100000+00000014=00100014
printf("%p\n",(unsigned long)p+0x1);//00100001 p The pointer type of is converted to an integer +1 Is simply add 1
printf("%p\n",(unsigned int*)p+0x1);//00100004 p Convert to integer pointer ,P+1 Just walk by 4 byte
return 0;
}

Third question
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[4] = {
1,2,3,4};
int* ptr1 = (int*)(&a+1);//&a Get the address of the entire array ,&a+1 Skip the entire array
int* ptr2 = (int*)((int)a+1);
printf("%x\n",ptr1[-1]);//4 ptr[-1]==*(ptr+(-1))==*(ptr-1)
// Array pointer strongly typed to integer pointer ptr-1, Point to last element
printf("%x\n",*ptr2);//2 00 00 00 Pay attention to small end storage
return 0;
}


Fourth question
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[3][2] = {
(0,1),(2,3),(4,5)};// Note that there are parentheses inside the parentheses , It is not the use of braces and comma operators
//a[3][2]{(0,1),(2,3),(4,5)}=={1,3,5}=={1,3,5,0,0,0}={
{1,3,5},
// {0,0,0}}
int* p = a[0];//p Is the pointer to the first row of the array
printf("%d\n",p[0]);//p[0]== The value of the first element in the first line
return 0;
}

Fifth question
#include<stdio.h>
#include<stdlib.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]);
}


Sixth question
#include<stdio.h>
#include<stdlib.h>
int main()
{
int aa[2][5] = {
1,2,3,4,5,6,7,8,9,10};
int *ptr1 = (int*)(&aa + 1);//&aa Represents the address of the entire array ,&aa+1: Skip the entire array
int *ptr2 = (int*)(*(aa + 1));//aa Represents the first element of the array , The first element is a one-dimensional array ;
//aa+1: The address of the first element in the second line
//*(aa+1) The first element in the second line
printf("%d\n",*(ptr1-1));//10
printf("%d\n",*(ptr2-1));//5
return 0;
}

Question seven
#include<stdio.h>
#include<stdlib.h>
int main()
{
char* a[] = {
"work","at","alibaba"};
char** pa = a;
pa++;
printf("%s\n",*pa);
return 0;
}


The eighth question
#include<stdio.h>
#include<stdlib.h>
int main()
{
char* c[] = {
"ANTER","NEW","POINT","FIRST"};
char** cp[] = {
c + 3,c + 2,c + 1,c};
char*** cpp = cp;
printf("%s\n",**++cpp);//POINT
//cpp+1 Point to c+2, After two dereferences POINT
printf("%s\n", *--*++cpp + 3);//ER
//++cpp:cpp Point to c+1
//*(++cpp): obtain c The contents of the pointer to the second element of the pointer array
//--(*(++cpp)): The second element pointer -1, Get the contents of the first element pointer
//*(--(*(++cpp))): Dereference to get ENTER
//*(--(*(++cpp)))+3: Point to T, Output TR
printf("%s\n",*cpp[-2]+3);//ST
// Be careful cpp So far, it still points to c+1,cpp[-2] The content of the address pointed to changes ,cpp The direction of has not changed
//*cpp[-2]==*(*(cpp+(-2))) therefore *cpp[-2] Point to ,c+3, Get is FIRST
//*cpp[-2]+3 Point to S
printf("%s\n",cpp[-1][-1]+1);//EW
// Be careful cpp So far, it still points to c+1
//cpp[-1][-1]+1 == *(*(cpp-1)-1)+1
//*(cpp-1) Point to c+2;
//*(*(cpp-1)-1) Point to c[2] The content of ;
//*(*(cpp-1)-1)+1 Point to NEW Medium N
return 0;
}


边栏推荐
- Mixin -- mixed
- Rmarkdown easily input mathematical formula
- How to add aggregation hotspots in VR panorama? How to add a content module?
- Datascience & ml: detailed introduction to risk control indicators / field related concepts and dimension logic of risk control in the field of financial technology
- Esp8266 system environment setup
- VR全景作品中各式各样的嵌入功能是如何做到的?
- Design and implementation of SSM based traffic metering cloud system Rar (thesis + project source code)
- Sliding window maximum value problem
- 领域驱动设计(六) - 架构设计浅谈
- SAR image focusing quality evaluation plug-in
猜你喜欢

大学生毕业季找房,VR全景看房帮你线上筛选
![[web] personal homepage web homework](/img/16/90b7b559e43e7cd6d5e32865eb0c00.png)
[web] personal homepage web homework "timetable", "photo album" and "message board"

Super simple course design SSM student management system (including simple addition, deletion, modification and query of source code)

如何在VR全景作品中添加独立热点?

How do various embedded functions in VR panoramic works be achieved?

【C语言进阶】数据的存储【上篇】【万字总结】

高考结束,VR全景云游为你展现景区的美好风光

Basic improvement - tree DP supplement

Computer: successfully teach you how to use one trick to retrieve the previous password (the password once saved but currently displayed as ******)

Yuntu says that every successful business system cannot be separated from apig
随机推荐
SAR image focusing quality evaluation plug-in
SPSS introductory notes
[advanced C language] advanced pointer [Part 2]
Harbor image pull voucher configuration
专项测试之「 性能测试」总结
Deep understanding of lightgbm
618 great promotion is coming, mining bad reviews with AI and realizing emotional analysis of 100 million comments with zero code
Domain Driven Design (VI) - Architecture Design
一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
OFFICE技术讲座:标点符号-中文-大全
叮咚抢菜-派送时段监听及推送工具
全数字时代,企业IT如何完成转型?
Which school do you choose after the college entrance examination? VR panoramic campus all-round display
騰訊Libco協程開源庫 源碼分析(二)---- 柿子先從軟的捏 入手示例代碼 正式開始探究源碼
写作技术文章是留给自己未来的财富
How to add independent hotspots in VR panoramic works?
MATLAB 根据任意角度、取样点数(分辨率)、位置、大小画椭圆代码
2022 software test interview strategy for the strongest version of fresh students to help you get directly to the big factory
Tidb - quick start, cluster setup
VR全景作品中各式各样的嵌入功能是如何做到的?