笔试题一:
int main() { int a[5] = { 1,2,3,4,5 }; int* ptr = (int*)(&a + 1); printf("%d %d", *(a + 1), *(ptr - 1)); return 0; }
输出结果:2 5
笔试题二:
struct test { int Num; char* pcName; short sDate; char cha[2]; short sBa[4]; }* p; //已知,结构体test类型的变量大小为20个字节 int main() { p = (struct test*)0x00100000; printf("%p\n", p + 0x1); printf("%p\n", (unsigned long)p + 0x1); printf("%p\n", (unsigned int*)p + 0x1); return 0; }
输出结果:
0x00100014
0x00100001
0x00100004
笔试题三:
(在大端存储的条件下)
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; }
结果:
4,2000000
笔试题四:
int main() { int a[3][2] = { (0,1),(2,3),(4,5) }; int* p; p = a[0]; printf("%d", p[0]); return 0; }
分析:在初始化时,里面用的是圆括号,所以里面放的是三个逗号表达式,实际上在数组初始化时只放入了三个元素。
输出结果:
1
笔试题五:
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; }
(指针减指针)大地址减小地址算出来的是这两个指针之间元素的个数。
输出结果:
FFFFFFFC,-4
笔试题六:
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; }
输出结果:
10,5
笔试题七:
int main() { char* a[] = { "work","at","ablibaba" }; char** pa = a; pa++; printf("%s\n", *pa); return 0; }
输出结果:
POINT
ER
ST
EW