当前位置:网站首页>[dish of learning notes, dog learning C] Dachang written test, is that it?
[dish of learning notes, dog learning C] Dachang written test, is that it?
2022-07-24 04:14:00 【Jiang Junzhu】
Topic 1 :
// What is the result of the program ?
int main(){
int a[5] = {
1, 2, 3, 4, 5 };
int *ptr = (int *)(&a + 1);
printf( "%d,%d", *(a + 1), *(ptr - 1));
return 0;
}
int main(){
int a[5] = {
1, 2, 3, 4, 5 };
int *ptr = (int *)(&a + 1);
//&a Get the address of the entire array ,+1 Skip this array and point to the next address
//(int *) Strong transition type
printf( "%d,%d", *(a + 1), *(ptr - 1));
//a+1 Represents the address of the second element , Dereference to get the second element , That is to say 2
//ptr - 1 Get the address of the fifth element , Dereference to get the fifth element , That is to say 5
return 0;
}

Topic two :
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}*p;
// hypothesis p The value of is 0x100000. What are the values of the expressions in the following table ?
int main()
{
printf("%p\n", p + 0x1);
printf("%p\n", (unsigned long)p + 0x1);
printf("%p\n", (unsigned int*)p + 0x1);
return 0;
}
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}*p;
//1 individual int4 byte ,1 individual char*4 byte ,1 individual short2 byte
//1 Two elements char Array 2 byte ,1 Four elements short Array 8 byte
// So the size of this structure is 4+4+2+2+8=20 byte
int main()
{
printf("%p\n", p + 0x1);
//0x1 Lower hexadecimal representation 1
//p It's a pointer to a structure ,p The value of is 0x100000
// Adding one is equivalent to adding 20,20 In hexadecimal, it is 14
// So the result is 0x100014
printf("%p\n", (unsigned long)p + 0x1);
//(unsigned long) hold p The type of , Plus one is plus one
// The result is 0x100001
printf("%p\n", (unsigned int*)p + 0x1);
//(unsigned int*) hold p Strong into a int Pointer to type
// Plus one is equal to plus four
// The result is 0x100004
return 0;
}

Question three :
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;
}
int main()
{
int a[4] = {
1, 2, 3, 4 };
int* ptr1 = (int*)(&a + 1);
//&a Get the address of the entire array ,+1 Skip the entire array
//int* Strong transition type ,ptr1 Only four bytes are skipped for each addition and subtraction
int* ptr2 = (int*)((int)a + 1);
//a Represents the address of the first element , Be forced to int type , It's a string of binary numbers
//+1, This is the string of binary numbers +1, It is equivalent to changing one byte
//int* Strong transition type , Length programming 4 Bytes
printf("%x,%x", ptr1[-1], *ptr2);
//ptr[-1] amount to *(ptr1-1), The result is 4, Print in hexadecimal or 4
//*ptr2 The resulting Hex is 2000000
return 0;
}

Question 4 :
int main()
{
int a[3][2] = {
(0, 1), (2, 3), (4, 5) };
int* p;
p = a[0];
printf("%d", p[0]);
return 0;
}
int main()
{
int a[3][2] = {
(0, 1), (2, 3), (4, 5) };
// Be careful !!! Here is () No {}, Who is deceived? I don't speak (doge)
// therefore a[3][2] What is actually stored is { 1, 3, 5}
int* p;
p = a[0];
//p Stored is an array a[3][2] The first line of the address
printf("%d", p[0]);
//p[0] amount to *p, namely *a[0], Get the first element in the first line , That is to say 1
return 0;
}

Question five :
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;
}
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]);
// Subtracting two pointers yields the number of elements between them ,&p[4][2] And &a[4][2] The number of elements between is 4
// But the address is from low to high , So the result is -4
// Negative numbers are stored in memory as complements
//-4 Complement code :11111111 11111111 11111111 11111100
//%p Print as a pointer , It doesn't matter what complement the original code will
// Print this string of binary directly in hexadecimal form , The result is FF FF FF FC
//%d The printed value is -4
return 0;
}

Question six :
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;
}
int main()
{
int aa[2][5] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int* ptr1 = (int*)(&aa + 1);
//&aa Get the address of the entire array ,+1 Skip this array
//int* Strong transition type ,ptr1 The length is 4 Bytes
int* ptr2 = (int*)(*(aa + 1));
//aa Indicates the address of the first line ,+1 Get the second line of address
// Dereference to get the address of the first element in the second line
//(int*) It's a smoke bomb , The result is the same with or without it
printf("%d,%d", *(ptr1 - 1), *(ptr2 - 1));
//ptr1-1 Get the address of the fifth element in the second line , Dereference to get 10
//ptr2-1 Get the address of the fifth element in the first line , Dereference to get 5
return 0;
}

Question seven :
int main()
{
char* a[] = {
"work","at","alibaba" };
char** pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}
int main()
{
char* a[] = {
"work","at","alibaba" };
//"alibaba" Focus on , It shows that this is Ali's problem (doge)
//char* type , So the address of the first letter of three words is stored
char** pa = a;
//a Represents the address of the first element , One char* The address of , Store with secondary pointer
pa++;
//pa++, Point to the second element
printf("%s\n", *pa);
//pa Dereference to get string “at” The first letter “a” The address of
//%s, from “a” Start printing , encounter “\0” stop it
// The string will automatically add a “\0”
// So the result is “at”
// Ali ? Is this ? I , Would like to be doing ! Tomorrow, ,offer! understand ?
return 0;
}

Question eight :
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;
}
int main()
{
char* c[] = {
"ENTER","NEW","POINT","FIRST" };
//char* type , So the address of the first letter of three words is stored
char** cp[] = {
c + 3,c + 2,c + 1,c };
char*** cpp = cp;
//cpp Deposit cp First element address
printf("%s\n", **++cpp);
//++cpp Deposit cp The address of the second element ,cpp The stored value has changed
// Dereference to get c+2 namely c The address of the third element
// Dereference again to get ‘P’ The address of
// be %s Print out "POINT"
printf("%s\n", *-- * ++cpp + 3);
//cpp Deposit is cp[1] The address of ,++cpp Point to cp[2]
// Dereference to get “c+1”,--(c+1) obtain c, Point to ‘E’ The address of
//‘E’+3 Get a second ‘E’ The address of
//%s From the second ‘E’ Start printing , The result is “ER”
printf("%s\n", *cpp[-2] + 3);
//cpp Deposit is cp[2] The address of ,cpp[-2] namely *(cpp-2), obtain “c+3”
// Dereference again , obtain ‘F’ The address of
//‘F’+3 obtain ‘S’ The address of
//%s from ‘S’ Start printing , The result is “ST”
printf("%s\n", cpp[-1][-1] + 1);
//cpp[-1][-1] amount to *(*(cpp-1)-1)
//*(cpp-1) obtain “c+2”,c+2-1 == c+1
//*(c+1) obtain ‘N’ The address of
//‘N’+1 obtain ‘E’ The address of
//%s from ‘E’ Start printing , The result is ‘EW’
return 0;
}




边栏推荐
- Svg image color modification is not fancy
- [development technology] spingboot database and Persistence technology, JPA, mongodb, redis
- Shell syntax (2)
- MOS cameraization and digitization "includes designation (contro. skilled
- Educational Codeforces Round 132 A - D
- Baidu search cracking down on pirated websites: why Internet content infringement continues despite repeated prohibitions
- Leetcode 20 valid parentheses, 33 search rotation sort array, 88 merge two ordered arrays (nums1 length is m+n), 160 intersecting linked list, 54 spiral matrix, 415 character addition (cannot be direc
- Text attack methods open source code summary
- Pat grade a 1043 is it a binary search tree
- 一次 svchost.exe 进程占用大量网络带宽的排查
猜你喜欢

Shell语法(一)

svg图片颜色的修改 没有花里胡哨

Design and implementation of data analysis platform for intelligent commerce

NFT insider 67: Barcelona Football Club launched its first NFT work, and Dubai launched the national metauniverse strategy

Three cluster schemes of redis
![[novice] develop a tab component that is easy to expand by hand](/img/67/ac0a0a4861f933e447a84c24f62e79.gif)
[novice] develop a tab component that is easy to expand by hand

What if Adobe pr2022 doesn't have open subtitles?

Worthington mammalian lactate dehydrogenase study -- Characteristics and determination scheme

Iqoo 10 series attacks originos original system to enhance mobile phone experience

Pat grade a 1041 be unique
随机推荐
发送数据1010_1发人员通过 字节的
Ros2 common command line tools organize ros2cli
Sqlserver backup restore
buu web
【望解答】数据无法正确同步了
训练数据量不只适用于.z据接收方对数字视
How to protect JDBC applications from SQL injection
[translation] chaos mesh moved to CNCF incubator
Embedded system transplantation [6] - uboot source code structure
mongo从开始到安装以及遇到的问题
[09] program loading: "640K memory" is really not enough?
Analyze the real-time development method of Bank of London
Therefore, the command can be transmitted to the system and confirmed by the user. For master
eCB接口,其实质也 MDSemodet
PAT甲级 1041 Be Unique
NFT insider 67: Barcelona Football Club launched its first NFT work, and Dubai launched the national metauniverse strategy
"PostgreSQL guide -- inside exploration" Chapter 2 process and memory architecture
MOS摄像化、数字化”包含指定(contro.熟练的
From bio to realizing the function of simple multi person chat room -- IO model
Live broadcast preview | practice sharing of opengauss' autonomous operation and maintenance platform dbmind