catalogue
First question
int main() { int a[5] = { 1, 2, 3, 4, 5 }; int *ptr = (int *)(&a + 1); printf( "%d,%d", *(a + 1), *(ptr - 1)); return 0; }
A is the address of the first element of the array, so * (a + 1) is the second element 2 of the array.
ptr is an integer pointer variable. It stores & A + 1, that is, the address after skipping the whole array, and then * after (int *) - 1, so the result is 5
Second question
struct Test { int Num; char *pcName; short sDate; char cha[2]; short sBa[4]; }*p; //Suppose the value of p is 0x100000. What are the values of the expressions in the following table? //It is known that the variable size of the structure Test 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; }
p + 0x1 because p is a pointer to a structure with a size of 20 bytes, + 0x1 will skip 20 bytes in the address and print it in the form of address, which is equal to 0x100014
After p is (unsigned long) and + 0x1, it is just a simple unsigned long integer + 1, and the result is 0x100001
After p is (unsigned int *), and then + 0x1, 4 bytes are skipped, and the result is 0x100004
Question 3
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; }
&A + 1 is the address after skipping the a array and the type is int (*) [4], and after (int *), ptr1 is an integer pointer. Therefore, after ptr1 [-1], the result is 4, and then the result printed in hexadecimal should be 4
Because my compiler is small end storage, the storage of array a in memory is
01 00 00 00 ,02 00 00 00 ,03 00 00 00 ,04 00 00 00
Therefore, after the address of the first element of a is (int) and + 1, the actual operation of (int *) is to move the access of ptr2 by one byte, then the address space she accesses should be 00 00 02, that is, the one printed in hexadecimal is 2000000
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; }
a[0] is the address of the first element in the first row of the array and assigned to P, so p[0] is the first element of the array (0, 1). Because the comma expression takes the value of the last expression, the result of changing the code to 1
Question 5
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; }
p[4][2] is * (* (p + 4) + 2), and p is a pointer to an integer array with 4 elements. What she skips with + 4 is 4 * 4 * bytes. After * she gets an integer array with 4 elements, and she represents it as the address of the first element of the array. Therefore, after + 2 and * she is the 18th element of array a.
a[4][2] is the second element in the fourth row of the array, that is, the fourth * 5 + 2 element of the a array
To sum up, & p [4] [2] - [a [4] [2] = = - 4. If printed in the form of% p, it should be the hexadecimal complement. The original inverse complement of - 4 is:
10000000 00000000 00000000 00000100
11111111 11111111 11111111 11111011
11111111 11111111 11111111 11111100
So the result of the whole code is FF FC, - 4
Question 6
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; }
ptr1 refers to the integer address after the entire aa array;
The space of ptr2 is the integer address of the first element of the second line of the array;
So the result of the code is 10,5
Question 7
#include <stdio.h> int main() { char *a[] = {"work","at","alibaba"}; char**pa = a; pa++; printf("%s\n", *pa); return 0; }
a[0] == " work " , a[1] == " at " , a[3] == " alibaba ";
pa == a;
pa++ == a++;
Therefore, * pa = = * (a + +) is "at"
Question 8
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; }
++After cpp, cp + 1 is stored in cpp, c + 2 is stored after dereference, and then "POINT" is dereferenced
++After cpp, cp + 2 is stored in cpp, c + 1 is stored after dereference, - c is stored after dereference, ENTER is stored after dereference, and + 3 is equal to the address of 'E', so the result is "ER"
After cpp [-2], it is equal to * cp, that is, c + 3. After dereferencing again, it is "FIRST". After + 3, it is equal to the address of'S', and the result is "ST"
After cpp [-1], it is equal to * (cp + 1), that is, c + 2. After [- 1] again, you get "NEW", so the result after + 1 is the address of "E", so you can get "EW" by outputting% s