- Prolog
An angel took the STAT580 course of ISU, so I wrote for her.
The following books are good and easy to read and learn C and C++.
-
Davis, S.R. (1998),
C++ For Dummies, 3rd,
IDG Books Worldwide.
-
Rudd, A. (1994),
Mastering C,
John Wiley and Sons.
- Story
The idea of
Pointer was the most confusion of C language for me when I learned it.
Even now, I am not sure I can fully understand and manipulate it well.
The following wordy example that I wrote for a friend is a useful code
to clarify the idea by several tests.
Key:
A POINTER IS A INTERGER
(repeat 10 times.)
- Code
You can download the code
"a.c"
and compile it by typing gcc a.c
and execute it by typing
./a.out
.
- Output
The output of "a.out" will similar to
the following or
"a.c.out".
Note that the memory address at the following output
should be different with your results.
```
Test 1
MSG: Initial value: a1 = 1, a2 = 1
aa MSG: Value of aa1: 1
aa MSG: After a change, value: aa1 = 2
MSG: after a change: a1 = 1, a2 = 2
------------
Test 2 (Pass "the address of b1" to function bb)
MSG: Initial value: b1 = 1, b2 = 1
MSG: Address of b1 (&b1): bfc585d4
bb MSG: Value of bb1: bfc585d4
(This should be the address of b1)
bb MSG: Address of bb1 (&bb1): bfc585b0
bb MSG: After a change, value: *bb1 = 2 (changed)
MSG: after a change: b1 = 2, b2 = 2
(These should be equal to *bb1)
------------
Test 3
MSG: Initial value: c1 = &b1 = bfc585d4
(This should be equal to the address of b1)
c1 point to b1 which has same value *c1 = b1 = 2
(This should be equal to the value of b2)
c1 is at address = bfc585d0
(As Test2, but pass "c1" to function bb)
bb MSG: Value of bb1: bfc585d4
(This should be the address of b1)
bb MSG: Address of bb1 (&bb1): bfc585b0
bb MSG: After a change, value: *bb1 = 4 (changed)
(This should be twice of b1 in the first Test 2)
MSG: after a change: b1 = 4, b2 = 2
(These should be equal to *bb1)
(As Test1, but pass "*c1" to function aa)
aa MSG: Value of aa1: 4
aa MSG: After a change, value: aa1 = 8
MSG: After a change: *c1 = 4 (no changed), a3 = 8
------------
Test 4
MSG: array d1 = {6 4 2}
```
---