TestPointer

时间:2023-03-09 01:19:57
TestPointer

 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;

])
{
    // 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
};
    ];
    p = a;
    fun(a);

// test sizeof
    // for a
    cout << sizeof(a) << endl; // 24
) << endl; // 4  (24 in debug local watch)
) << endl; // 4  (24 in debug local watch)

cout << ]) << endl; // 12
]) << endl; // 12
)) << endl; // 12
)) << endl; // 12
]) << endl; // 4

// for p
    cout << sizeof(p) << endl; // 4
) << endl; // 4
) << endl; // 4

cout << ]) << endl; // 12
]) << endl; // 12
)) << endl; // 12
)) << endl; // 12
]) << endl; // 4

// pointer array
];
    ;
    ;
    r[] = &i;
    r[] = &j;
    cout << sizeof(r) << endl; // 8
}

void test_1dim()
{
    };
    int *p = a;
    int *p1 = (int *)&a;
    );

]; // array pointer
    b = &a;
    int *b1 = (int *)b;
    );

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
] = 'Y'; // right
    cout << *str2 << endl; // Y
}

void funstr(char str[])
{
    printf("After transform:%d\n", sizeof(str));  //4
}

void test_array_as_param()
{
    char strs[] = "abcdefg";
    printf("Before transform:%d\n", sizeof(strs)); //8
    funstr(strs);
}

void test_a()
{
    // (int&)a treat a memory as a integer
.0f;
    cout << (int)a << endl;  // 1
    cout << (int &)a << endl; // 1065353216
    cout << boolalpha << ( (int)a == (int &)a ) << endl; // false

.0f;
    cout << (int)b << endl; // 0
    cout << (int &)b << endl; // 0
    cout << boolalpha << ( (int)b == (int &)b ) << endl; // true
}