C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
/*
version: 1.0 author: hellogiser blog: http://www.cnblogs.com/hellogiser date: 2014/6/3 */ #include "stdafx.h" #include <iostream> using namespace std; void fun(int a[][3]) { // when a is passed to function,a will become a array pointer // like int (*p)[3], so sizeof(a)==4 cout << sizeof(a) << endl; // 4 } void test_2dim() { // array pointer int a[2][3] = {1, 2, 3, 4, 5, 6}; int (*p)[3]; p = a; fun(a); // test sizeof // for a cout << sizeof(a) << endl; // 24 cout << sizeof(a + 0) << endl; // 4 (24 in debug local watch) cout << sizeof(a + 1) << endl; // 4 (24 in debug local watch) cout << sizeof(a[0]) << endl; // 12 cout << sizeof(a[1]) << endl; // 12 cout << sizeof(*(a + 0)) << endl; // 12 cout << sizeof(*(a + 1)) << endl; // 12 cout << sizeof(a[0][0]) << endl; // 4 // for p cout << sizeof(p) << endl; // 4 cout << sizeof(p + 0) << endl; // 4 cout << sizeof(p + 1) << endl; // 4 cout << sizeof(p[0]) << endl; // 12 cout << sizeof(p[1]) << endl; // 12 cout << sizeof(*(p + 0)) << endl; // 12 cout << sizeof(*(p + 1)) << endl; // 12 cout << sizeof(p[0][0]) << endl; // 4 // pointer array int *r[2]; int i = 1; int j = 2; r[0] = &i; r[1] = &j; cout << sizeof(r) << endl; // 8 } void test_1dim() { int a[2] = {1, 2}; int *p = a; int *p1 = (int *)&a; int *p2 = (int *)(&a + 1); int (*b)[2]; // array pointer b = &a; int *b1 = (int *)b; int *b2 = (int *)(b + 1); cout << "ok" << endl; } void test_string() { char *str1 = "abcd"; char str2[] = "1234"; cout << str1 << endl; // abcd cout << str2 << endl; // 1234 cout << *str1 << endl; // a cout << *str2 << endl; // 1 //str1[0]='X'; // ERROR str2[0] = 'Y'; // right cout << *str2 << endl; // Y } void funstr(char str[]) { printf("After transform:%d ", sizeof(str)); //4 } void test_array_as_param() { char strs[] = "abcdefg"; printf("Before transform:%d ", sizeof(strs)); //8 funstr(strs); } void test_a() { // (int&)a treat a memory as a integer float a = 1.0f; cout << (int)a << endl; // 1 cout << (int &)a << endl; // 1065353216 cout << boolalpha << ( (int)a == (int &)a ) << endl; // false float b = 0.0f; cout << (int)b << endl; // 0 cout << (int &)b << endl; // 0 cout << boolalpha << ( (int)b == (int &)b ) << endl; // true } |