cpp-pointers

JSON

rev 9  |  foxhop  |  1317067371000  |  JSON

rev 8
rev 9
49.. code-block:: cpp49.. code-block:: cpp
5050
n51 int size = 5; // create an integern51 int size = 5;         // create an integer
52 int *A;       // create an array of pointers52 int *A;               // create an array of pointers
5353
n54 A = new int[ size ] n54 A = new int[ size ];  // Ask for a certain size for your array
5555
tt56 A[0] = 10;            // Give values to Array
57 A[1] = 20;
58 A[2] = 30;
59 A[3] = 40; 
60 A[4] = 50; 
61 
62 //       8    32   32       36       40
63 cout << &A << A << A + 0 << A + 1 << A + 2
64 
65 //       10      28      30
66 cout << A[0] << A[1] << A[2] 
67 
68 //       10      10       28        30
69 cout << *A << *A + 0 << A + 1 << * A + 2
70 
71 delete []A;
rev 8  |  foxhop  |  1317066901000  |  JSON

rev 7
rev 8
4444
4545
nn46Dynamic Allocation for arrays
47--------------------------------------------
4648
tt49.. code-block:: cpp
50 
51 int size = 5; // create an integer
52 int *A;       // create an array of pointers
53 
54 A = new int[ size ] 
55 
rev 7  |  foxhop  |  1317066530000  |  JSON

rev 6
rev 7
20.. code-block:: cpp20.. code-block:: cpp
2121
n22 int *p, *q;n22 int *p, *q; // create two pointers, named p and q
23 23 
n24 p = new int;n24 p = new int; // 
2525
26 *p = 100;26 *p = 100;
3535
36 delete p; // give memory that p occupied back to operating system36 delete p; // give memory that p occupied back to operating system
t37  t37 
38 p = NULL; // set p to memory address 038 p = NULL; // set p to memory address 0  
39 
40 delete q; // give memory that q occupied back to operating system
41 
42 q = NULL; // set q to memory address 0
43 
44 
45 
46 
rev 6  |  foxhop  |  1317066363000  |  JSON

rev 5
rev 6
3333
34 *q = *p;34 *q = *p;
nn35 
36 delete p; // give memory that p occupied back to operating system
35  37  
tt38 p = NULL; // set p to memory address 0
rev 5  |  foxhop  |  1317066223000  |  JSON

rev 4
rev 5
2929
30 *q = 200;30 *q = 200;
tt31 
32 q = new int;
33 
34 *q = *p;
31  35  
rev 4  |  foxhop  |  1317066138000  |  JSON

rev 3
rev 4
13 the content of p (or the address that p points to)13 the content of p (or the address that p points to)
1414
n15dynamic memory allocation or aliasn15Dynamic memory allocation or alias
16------------------------------------------16------------------------------------------
1717
18**Useful but dangerous...**18**Useful but dangerous...**
tt19 
19.. code-block:: cpp20.. code-block:: cpp
2021
rev 3  |  foxhop  |  1317066113000  |  JSON

rev 2
rev 3
16------------------------------------------16------------------------------------------
1717
tt18**Useful but dangerous...**
18.. code-block:: cpp19.. code-block:: cpp
1920
rev 2  |  foxhop  |  1317066067000  |  JSON

rev 1
rev 2
12p12p
13 the content of p (or the address that p points to)13 the content of p (or the address that p points to)
tt14 
15dynamic memory allocation or alias
16------------------------------------------
17 
18.. code-block:: cpp
19 
20 int *p, *q;
21 
22 p = new int;
23 
24 *p = 100;
25 
26 q = p;
27 
28 *q = 200;
29  
rev 1  |  foxhop  |  1317065733000  |  JSON

empty
rev 1
tt1cpp Pointers
2==============
3 
4**if p is a pointer**
5 
6&p
7 the memory address of p
8 
9\*p
10  the content that p points to
11 
12p
13 the content of p (or the address that p points to)