1.
#includeint 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; }
分析一下这串代码:
在这里,我们分析清楚了ptr1 和ptr2指向的是哪里。
接下来我们看看打印的这两个是什么样子的东西。
看打印结果:
2.#includeint main() { char *c[] = { "ENTER", "NEW", "POINT", "FIRST" }; char **cp[] = { c + 3, c + 2, c + 1, c }; char ***cpp = cp; printf("%sn", **++cpp); printf("%sn", *--*++cpp + 3); printf("%sn", *cpp[-2] + 3); printf("%sn", cpp[-1][-1] + 1); return 0; }
这段代码的结果是什么呢?
来看一下最后的打印结果~~
3.#includeint main() { int a[3][2] = { (0, 1), (1, 2), (2, 3) }; int *p; p = a[0]; printf("%d", p[0]); return 0; }
来看看打印结果
4.#includeint main() { int a[5][5]; int(*p)[4]; p = (int(*)[4])a; printf("%p %dn", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]); return 0; }
来看一下打印结果
5.#includeint 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 %dn", *(ptr1 - 1), *(ptr2 - 1)); return 0; }
来看一下运行结果
6.int main() { int a[] = { 1, 2, 3, 4 }; printf("%dn", sizeof(a)); printf("%dn", sizeof(a + 0)); printf("%dn", sizeof(*a)); printf("%dn", sizeof(a + 1)); printf("%dn", sizeof(a[1])); printf("%dn", sizeof(&a)); printf("%dn", sizeof(*&a)); printf("%dn", sizeof(&a + 1)); printf("%dn", sizeof(&a[0])); printf("%dn", sizeof(&a[0] + 1)); return 0; }
字符串1
#include#include int main() { char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; printf("%dn", sizeof(arr)); printf("%dn", sizeof(arr + 0)); printf("%dn", sizeof(*arr)); printf("%dn", sizeof(arr[1])); printf("%dn", sizeof(&arr + 1)); printf("%dn", sizeof(&arr[0] + 1)); printf("%dn", strlen(arr)); printf("%dn", strlen(arr+0)); printf("%dn", strlen(*arr)); printf("%dn", strlen(arr[1])); printf("%dn", strlen(&arr)); printf("%dn", strlen(&arr + 1)); printf("%dn", strlen(&arr[0] + 1)); return 0; }
打印结果
字符串2
#include#include int main() { char arr[] = "abcdef"; printf("%dn", sizeof(arr)); printf("%dn", sizeof(arr + 0)); printf("%dn", sizeof(*arr)); printf("%dn", sizeof(arr[1])); printf("%dn", sizeof(&arr + 1)); printf("%dn", sizeof(&arr[0] + 1)); printf("%dn", strlen(arr)); printf("%dn", strlen(arr+0)); printf("%dn", strlen(*arr)); printf("%dn", strlen(arr[1])); printf("%dn", strlen(&arr)); printf("%dn", strlen(&arr + 1)); printf("%dn", strlen(&arr[0] + 1)); return 0; }
打印结果
指针
#includeint main() { char *p = "abcdef"; printf("%dn", sizeof(p)); printf("%dn", sizeof(p + 1)); printf("%dn", sizeof(*p)); printf("%dn", sizeof(p[0])); printf("%dn", sizeof(&p)); printf("%dn", sizeof(&p + 1)); printf("%dn", sizeof(&p[0] + 1)); printf("%dn", strlen(p)); printf("%dn", strlen(p + 1)); printf("%dn", strlen(*p)); printf("%dn", strlen(p[0])); printf("%dn", strlen(&p)); printf("%dn", strlen(&p+1)); printf("%dn", strlen(&p[0] + 1)); return 0; }
来看打印结果
二维数组
#includeint main() { int a[3][4] = { 0 }; printf("%dn", sizeof(a)); printf("%dn", sizeof(a[0][0])); printf("%dn", sizeof(a[0])); printf("%dn", sizeof(a[0]+1)); printf("%dn", sizeof(*(a[0]+1))); printf("%dn", sizeof(a+1)); printf("%dn", sizeof(*(a+1))); printf("%dn", sizeof(&a[0] + 1)); printf("%dn", sizeof(*(&a[0]+1))); printf("%dn", sizeof(*a)); printf("%dn", sizeof(a[3])); return 0; }
打印结果:
over~~~
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)