For the accuracy of the topic and our general habits in the learning process , All the title codes here are in X86 environment (32 Bit platform ) Running under .

Topic 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; } // What is the output of the program ?
Let's study it first ptr What is stored in the pointer variable .&a+1 Represents the size of taking out the entire address and jumping back an array type , That is, it points to the last element of the array 5
Space of the size of the last array type of .

But we want to give this address to the pointer variable ptr No way , Because the types are different (&a+1 Is an array pointer type ,prt Is an integer pointer type ), So we are (&a+1)
before (int*) To force type conversion . So we can figure it out ptr What is stored in the pointer variable .

Now let's look at the output part .*(a+1) It's relatively simple ,a Is the first element address ,a+1 Is the address of the second element , Dereference it to get the second element 2 , This is the undoubted answer . about
*(ptr-1) , We know ptr Address location stored inside , And know ptr Is an integer pointer ,ptr-1 It is equivalent to jumping forward the size of an integer type , That is to point to the last element of the array
5 Location of . Dereference it to get the element 5 .

 

  So the final output is : 2,5 .

Topic 2 :
#include <stdio.h> struct Test { int Num; char* pcName; short sDate; char
cha[2]; short sBa[4]; }*p; // What are the values of the expressions in the following table ? // Known , structural morphology Test The variable size of type is 20 Bytes int
main() { printf("%p\n", p + 0x1); printf("%p\n", (unsigned long)p + 0x1);
printf("%p\n", (unsigned int*)p + 0x1); return 0; } // What is the final output of the program ?
We pay attention to the definition of structure , Defines a structure pointer variable p .

p+0x1 It's simple ,0x1 namely 1, It's just written in hexadecimal format . This is the same as the pointer calculation we are often used to ,p Is a structure pointer , that p+1
It is reasonable to jump back the size of a structure type , That is, it points to the position of the next structure space in the structure space . And we know p Is a global variable , Global variables are initialized to by default 0
, Then we can know ,p=0 ,p+1=20 .  and %p Is a hexadecimal print format , So the result output :00000014 .

(unsigned long)p + 0x1 It seems simpler .p Type converted to a long integer , Then it means p No longer a pointer , It's stored 0
It's a simple number . therefore 0 + 0x1 Is equal to 00000001 .

(unsigned int*)p + 0x1 It's also very simple .p This is a structure pointer , Now force to integer pointer , That means p When adding and subtracting integers, the step length is 20 Bytes
Become 4 Bytes . that p=0 ,p+1=4 There is no doubt about it . The output result is :00000004 .

  Topic 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; } // What is the output of the program ?
ptr1 The analysis of is no different from topic one ,ptr1 It stores the address of the size of the array type after the last element of the array . also ptr1 Is an integer pointer .

ptr1[-1] Rewritable as *(ptr-1) , Because we once said , Any operation of array subscript is pointer operation , The operation of array subscript is only the abbreviation of pointer operation . So at this time
ptr1[-1] It should point to the last element of the array 4 And get the output result by dereferencing it 00000004 .

(int*)((int)a+1) How to understand it ?a Is the array name , Then it points to the address of the first element of the array , And then cast it , that a The address stored inside is simply a number
, Based on this figure +1 , Then cast the type into an integer pointer and store it in the ptr2 in . in other words ,a This is the address pointing to the first element of the array ,(int)a+1 It represents the number of addresses
+1 , Number of addresses +1 It means that if it is a pointer , It will point to the next byte . Re force to integer pointer , So at this time ptr2 The address pointed to is :

 

You can find the content of the red square space by dereferencing it , Because it is small end storage , So the output is :02000000 .

It should be noted that , The print format used for this question is %x , Hexadecimal of the eight meridians , Will omit the number before the significant number 0 .

Topic 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; } // What is the output of the program ?
First of all, we should pay attention to observation , Two dimensional array a There are three comma expressions stored inside , I believe someone will step on the pit here . Three comma expressions , Then two-dimensional array a The contents should be 1,3,5
,0,0,0 ( array a have 6 Elements , If the content is insufficient, it will be automatically supplemented later 0 ).

 

After figuring out what is stored in a two-dimensional array , Let's study a[0] What is it? .a[0] Is the first element of a two-dimensional array , This element is an array , in other words , Pointer variable p
It stores the first element in a two-dimensional array , This element is a one-dimensional array , That is, you get an array name . that p[0] It's easy to understand ,p Is an array name ,p[0] Is the first element of the array , Namely 1
. So output 1 .

 

 

Topic 5 :
#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; }
// What is the output of the program ?
We can see , Array pointer p Stored a two-dimensional array a First element address of , It's not difficult to analyze ,p Array pointed to and a The storage contents of the array are the same , But the memory allocation is different .

We overlap these two spaces :

  Can see ,&p[4][2] And &a[4][2] It's different 4 Elements , But because &p[4][2] The address location of is relatively low , So subtracting two is a negative number , Namely -4
. But we need to %p Print in the form of ,%p Is an unsigned number , So we know that the result should be a very large hexadecimal number . that -4 The original reverse complement of is :

So the output of the former is :fffffffc .

We know , Pointer ( address ) subtract , The result is the number of elements with phase difference , We also talked about it above . that %d Is a signed print format , So the output is -4 .

 

Topic 6 :
#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; } // What is the output of the program ?
*(ptr1-1) The result of is 10 There should be no problem ?

*(aa+1) Rewritable as aa[1] , That is, the second element of the two-dimensional array , This element is a one-dimensional array , That is, you get an array name
. Strongly convert the array name into an integer pointer and store it in the pointer variable ptr2 in , I'll know at this time ptr2 Specific pointing position of .( Two dimensional arrays are stored continuously in memory space !)

*(ptr2-1) Is to ptr2 Jump forward the size of an integer type , Merge and dereference , The element is found 5 , So the output result is 5 . 

Topic 7 :
#include <stdio.h> int main() { char* a[] = { "work","at","alibaba" }; char**
pa = a; pa++; printf("%s\n", *pa); return 0; } // What is the output of the program ?
Pointer array a Three string constants are stored ( Constants of strings have been introduced in previous blogs ), Use secondary pointer pa To point to this array (char** pa=a;), This shows that
pa Variables store a Address of the first element of the array .pa++ Points to the second element of the array ,*pa There is no doubt that the string constant is obtained "at" First element address of , Re pass %s
Print , You can get it at .

Topic 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; } // What is the output of the program ?
  Let's analyze it first c,cp,cpp What's in it . Pointer array c There are four string constants stored inside . Pointer array cp The addresses of four string constants are stored inside . Tertiary pointer
cpp Stored is Pointer array cp First element address of . Then the sketch can be drawn :

 

**++cpp , Because of the combination , We have to calculate first ++cpp, So at this time cpp point cp Address of the second element of , Then dereference and get cp
Second element in , This element points to c The third element of , And then dereference to get c The third element of , This element is a string constant "POINT" First character address of , adopt %s
Printed in the form of POINT . Be careful ,++ This operation is cpp Participate in the calculation , So the next operation will start from the calculated position .

 *--*++cpp+3 , Calculate first ++cpp , here cpp point cp Address of the third element of , Then dereference to get c+1 , then -- ,c+1 It becomes
c , Then dereference to get c First element address of , Based on this address +3 , You get the string constant "ENTER" Address of the fourth character of , Then pass %s Printed in the form of ER
.

 *cpp[-2]+3 Rewritable as *(*(cpp-2))+3 , in other words , here cpp Point to cp First element address of , Dereference got it c+3 ,c+3 Is pointing
c Address of the fourth element of , Dereference and get c The fourth element of , This element is a string constant "FIRST" First element address of , Based on this address +3
The address of the fourth character of the string constant is obtained ,%s Print through this address to get ST .( here cpp There is no substantive operation )

 cpp[-1][-1]+1 Rewritable as *(*(cpp-1)-1)+1 , here cpp point cp Address of the second element of , Obtained by dereference c+2 , then -1
obtain c+1 , It points to c Address of the second element of , Dereference and get c The second element of , This element is a string constant "NEW"  First element address of , then +1
It points to the address of the second character of the string constant , adopt %s Form printing is obtained EW .

 

 

Technology