当前位置:网站首页>c指针(面试经典题目练习)
c指针(面试经典题目练习)
2022-06-10 18:16:00 【缘友一世】
第一题
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[5] = {
1,2,3,4,5};
int* ptr = (int*)(&a + 1);//&a取得整个数组额地址,&a+1跳过整个数组
//数组指针强类型转化为整形指针
printf("%d,%d\n",*(a+1),*(ptr-1));//指针的加减看指针类型ptr为整形指针,所以 ptr-1指向数组的最后
return 0;
}

第二题
#include<stdio.h>
#include<stdlib.h>
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}* p;
//假设P的值为0x100000
//已知结构体test类型的变量大小是20个字节
int main()
{
p = (struct Test*)0x100000;
printf("%p\n",p+0x1);//00100014 p是结构体指针,p+1跳过整个结构体
//00100000+00000014=00100014
printf("%p\n",(unsigned long)p+0x1);//00100001 p的指针类型转化为整形+1就是单纯加1
printf("%p\n",(unsigned int*)p+0x1);//00100004 p转换为整形指针,P+1就是走过4字节
return 0;
}

第三题
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[4] = {
1,2,3,4};
int* ptr1 = (int*)(&a+1);//&a取得整个数组额地址,&a+1跳过整个数组
int* ptr2 = (int*)((int)a+1);
printf("%x\n",ptr1[-1]);//4 ptr[-1]==*(ptr+(-1))==*(ptr-1)
//数组指针强类型转化为整形指针ptr-1,指向最后一个元素
printf("%x\n",*ptr2);//2 00 00 00 注意小端存储
return 0;
}


第四题
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[3][2] = {
(0,1),(2,3),(4,5)};//注意打括号内是小括号,并不是大括号以及逗号运算符的使用
//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是第一行数组的的指针
printf("%d\n",p[0]);//p[0]==第一行第一个元素的值
return 0;
}

第五题
#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]);
}


第六题
#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代表整个数组的地址,&aa+1:跳过整个数组
int *ptr2 = (int*)(*(aa + 1));//aa代表数组的首元素,首元素是一个一维数组;
//aa+1:第二行首元素的地址
//*(aa+1) 第二行第一个元素
printf("%d\n",*(ptr1-1));//10
printf("%d\n",*(ptr2-1));//5
return 0;
}

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


第八题
#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指向c+2,两次解引用后找到POINT
printf("%s\n", *--*++cpp + 3);//ER
//++cpp:cpp指向c+1
//*(++cpp):得到c指针数组的第二个元素的指针的内容
//--(*(++cpp)):第二个元素指针-1,得到第一个元素指针的内容
//*(--(*(++cpp))):解引用得到ENTER
//*(--(*(++cpp)))+3:指向T,输出TR
printf("%s\n",*cpp[-2]+3);//ST
//注意cpp到目前为止还是指向c+1,cpp[-2]指向的地址内容变化,cpp的指向并未变化
//*cpp[-2]==*(*(cpp+(-2))) 所以*cpp[-2]指向,c+3,得到的是FIRST
//*cpp[-2]+3指向S
printf("%s\n",cpp[-1][-1]+1);//EW
//注意cpp到目前为止还是指向c+1
//cpp[-1][-1]+1 == *(*(cpp-1)-1)+1
//*(cpp-1)指向c+2;
//*(*(cpp-1)-1)指向c[2]的内容;
//*(*(cpp-1)-1)+1指向NEW中的N
return 0;
}


边栏推荐
- 第二章 数据类型(一)
- HelloWorld example of TestNG and how to run it from the command line
- JS Touch
- Chapter 161 SQL function year
- Use of uiautomator2 automated test tool
- Building smart community Internet of things based on smart road lamp posts
- 5. Golang泛型与反射
- Salesmartly | add a new channel slack to help you close the customer relationship
- 端午“沉浸式云旅游”怎么玩?即构助力“直播+”新场景落地
- openSSL1.1.1编译错误 Can‘t locate Win32/Console.pm in @INC
猜你喜欢

Adobe Premiere Foundation (track related) (V)

Metadata management, the basic construction of enterprises in the digital era

In the introductory study of data visualization, we should be alert to pitfalls and misunderstandings and grasp key nodes

libcurl 7.61.0 VS2013 编译教程

OPENCV 检测人脸 不依赖于任何第三方库

Adobe Premiere基础-导入导出,合并素材,源文件编译,脱机(二)

Openssl1.1.1 VS2013-编译教程

nodejs-基本架构分析-解析引擎目录-插件安装-核心模块

Data URL

Stream生成的3张方式-Lambda
随机推荐
VMware horizon 82111 deployment series (XVI) blast bandwidth test
[vulnhub range] janchow: 1.0.1
Wireshark learning notes (I) common function cases and skills
What is Bi? Talk about the definition and function of Bi
Stream生成的3张方式-Lambda
Db2 SQL PL的锚点类型和行数据类型
【杂谈】恭喜自己获得CSDN专家称号,努力终会换来结果
Ruixin micro rk1126 platform platform porting libevent cross compiling libevent
基于SSM流量计量云系统的设计与实现.rar(论文+项目源码)
OPENCV 检测人脸 不依赖于任何第三方库
第一章 SQL操作符
MySQL高级篇第一章(linux下安装MySQL)【上】
Adobe Premiere foundation - time remapping (10)
How to play the Dragon Boat Festival "immersive cloud Tour"? That is to say, it helps "live broadcast +" new scene landing
VS从txt文件读取中文汉字产生乱码的解决办法(超简单)
SQL语句查看基本表结构和表中约束字段 、主码、外码 (简单有效)
AgI foundation, uncertain reasoning, subjective logic ppt2
【 random talk 】 congratulations on getting the title of CSDN expert. Your efforts will eventually pay off
Classic 6 pain points of data governance? This book teaches you to solve
In the era of data processing, data analysis has become the basic construction