Statistics
| Branch: | Revision:

root / fiddle / pointer.cpp @ c28093c1

History | View | Annotate | Download (3.12 KB)

1
/*
2
TESTING POINTER
3
*/
4

    
5
#include <stdio.h>
6
#include <iostream>
7
using namespace std;
8

    
9
int func_reference(int& r)
10
{
11
    // cout << "*r = " << *r << endl; // ERROR
12
    cout << "&r = " << &r << endl;
13
    cout << "r = " << r << endl;
14
    r = 5;
15
    cout << "r = " << r << endl;
16
}
17

    
18
int main()
19
{
20
    int ** ramon;
21
    int *paul;
22
    int melissa = 5;
23
    paul = &melissa;
24
    ramon = &paul;
25

    
26
    printf("ramon = %d\n", ramon);
27
    printf("&paul = %d\n", &paul);
28
    printf("*ramon = %d\n", *ramon);
29
    printf("&melissa = %d\n", &melissa);
30
    printf("**ramon = %d\n", **ramon);
31

    
32
    int * xuan;
33
    xuan = &melissa;
34
    printf("*xuan = %d\n", *xuan);
35
    printf("&xuan = %d\n", &xuan);
36

    
37
    int* quynh;
38
    quynh = &melissa;
39
    printf("*quynh = %d\n", *quynh);
40
    printf("&quynh = %d\n", &quynh);
41

    
42
    const char * name = "ABCDE";
43
    std::cout << name;
44
    std::cout << "\n";
45

    
46
    // printf("&name = %s\n", &&name);
47
    printf("name = %s\n", name); // ABCDE
48
    printf("&(*name) = %s\n", &(*name)); // ABCDE
49
    printf("*name %%c = %c\n", *name); // A
50
    printf("*name %%u = %u\n", *name); // 65, some number, and it's the number of the address
51
    printf("*name %%d = %d\n", *name); // 65, some number, and it's the number of the address
52
    printf("(*name + 1) %%d = %d\n", (*name + 1)); // 66, some number, and it's the number of the address
53
    printf("(*name + 1) %%c = %c\n", (*name + 1)); // B
54
    printf("name[1] = %c\n", name[1]); // B
55
    cout << endl;
56

    
57
    cout << "******* Pass by reference ********" << endl << endl;
58
    int x = 16;
59

    
60
    cout << "# int arr[] = {7, 11, 13};" << endl;
61
    int arr[] = {7, 11, 13};
62
    cout << "arr = \t\t" << arr << endl;
63
    cout << "&(*arr)) = \t" << &(*arr) << endl;
64
    cout << "&arr = \t\t" << &arr << endl;
65
    cout << "&arr = " << long(&arr) << endl;
66
    cout << "??? why why why, plusing with 4, resulting in plusing with 16 ???" << endl;
67
    cout << "sizeof(int) = " << long(sizeof(int)) << endl;
68
    cout << "arr + sizeof(int) = " << long(arr + sizeof(int)) << endl;
69
    cout << "arr + 2*sizeof(int) = " << long(arr + 2*sizeof(int)) << endl;
70
    cout << "//" << endl;
71
    cout << "&arr + sizeof(int) = " << long(&arr + sizeof(int)) << endl;
72
    cout << "arr + 4 = " << long(arr + 4) << endl;
73
    cout << "arr + 8 = " << long(arr + 8) << endl;
74
    cout << "&arr[1] = " << &arr[1] << endl;
75
    cout << "*(&arr[1]) = " << *&arr[1] << endl;
76
    cout << "arr[1] = " << arr[1] << endl;
77
    cout << endl;
78

    
79
    cout << "# char char_arr[] = {'x', 'u', 'a', 'n', '\0'};" << endl;
80
    char char_arr[] = {'x', 'u', 'a', 'n', '\0'};
81
    cout << "char_arr = " << char_arr << endl;
82
    cout << "*char_arr = " << *char_arr << endl;
83
    cout << "&char_arr = " << &char_arr << endl;
84
    cout << "&(*char_arr) = " << &(*char_arr) << endl;
85
    cout << "??? Why char_arr is printed out completely, why arr only print number 10???";
86
    cout << endl << endl;
87

    
88
    cout << "### ['x', 'u', 'a', 'n']" << endl;
89
    char char_arr_2[] = {'x', 'u', 'a', 'n', '\0'};
90
    cout << "char_arr = " << char_arr_2 << endl;
91
    cout << "*char_arr = " << *char_arr_2 << endl;
92
    cout << "&(*char_arr) = " << &(*char_arr_2) << endl;
93

    
94
    func_reference(x);
95
    func_reference(*arr);
96
}