The document is a series of slides from a lecture on C programming pointers by Professor Joongheon Kim. It contains examples of using pointers with basic data types like integers and characters, as well as more complex examples involving pointers to arrays and functions. The examples are presented step-by-step with diagrams and explanations to illustrate how pointers store memory addresses and can be used to indirectly access and modify values in memory.
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
C Programming: Pointer (Examples)
1. Professor Joongheon Kim
https://joongheon.github.io
C Programming
C Programming
Pointer (Examples)
Directed by Prof. Joongheon Kim
Korea University, School of Electrical Engineering
Artificial Intelligence and Mobility Laboratory
https://joongheon.github.io
joongheon@korea.ac.kr
33. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers (Example 2.1)
• Pointer and Memory (char)
33
1
A
65 (= ‘A’)
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
c (char)
• ASCII code of “A”
It says sizeof(c) is 1.
It means “char” uses 1 Byte in memory.
• Address
(or Starting Address)
34. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers (Example 2.2)
• Pointer and Memory (int)
34
4
10
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
a (int)
It says sizeof(a) is 4.
It means “int” uses 4 Bytes in memory.
10
35. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers (Example 2.3)
• Pointer and Memory (double)
35
8
10
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
a (double)
It says sizeof(a) is 8.
It means “double” uses 8 Bytes in memory.
10
36. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers (Example 2.4)
• Pointer and Memory Address (char)
36
65 (= ‘A’)
66 (= ‘B’)
0x7fffc67cfb66
0x7fffc67cfb67
0x7fffc67cfb68
a (char)
0x7fffc67cfb66
0x7fffc67cfb67
0x7fffc67cfb67
0x7fffc67cfb68
b (char)
&a
&b
&a+1
&b+1
37. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers (Example 2.5)
• Pointer and Memory Address (int)
37
0x7ffdcbaf29d0
0x7ffdcbaf29d1
0x7ffdcbaf29d2
0x7ffdcbaf29d3
0x7ffdcbaf29d4
0x7ffdcbaf29d5
0x7ffdcbaf29d6
0x7ffdcbaf29d7
0x7ffdcbaf29d8
0x7ffdcbaf29d0
0x7ffdcbaf29d4
0x7ffdcbaf29d4
0x7ffdcbaf29d8
&a
&b
&a+1
&b+1
a (int)
1
b (int)
2
42. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers (Example 3.1)
• Double Pointer
42
A A A
0x7fff14e17fc3 0x7fff14e17fc3 0x7fff14e17fc3
=====
10 10 10
0x7fff14e17fc4 0x7fff14e17fc4 0x7fff14e17fc4
46. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers and Arrays (Introduction)
• Array x
• x: constant pointer
pointing the first element x[0]
• Thus, the value of x is 1000, i.e., x = &x[0] = 1000
• If we declare p as an integer pointer (int *p;),
the pointer p can point to the array x by p=x; (equivalent to p = &x[0])
• p = &x[0] // == 1000
p+1 = &x[1] // == 1004
p+2 = &x[2] // == 1008
p+3 = &x[3] // == 1012
p+4 = &x[4]
• *(p+3) gives the value of x[3], i.e., 4.
46
int x[5] = {1, 2, 3, 4, 5};
1 2 3 4 5
x[0] x[1] x[2] x[3] x[4]
1000 1004 1008 1012 1016
48. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers and Arrays (Character Strings)
• Character String x
48
char x[5] = “good”;
g o o d 0
x[0] x[1] x[2] x[3] x[4]
1000 1004 1008 1012 1016
char *x = “good”; x
53. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers and Functions (Example 5.1)
53
20
Int: x
0xf1
10
Int: y
0xcc
Int: s
0xfd
Int: d
0xdc
20
Int: a
10
Int: b
0xfd
Pointer
(Int): sum
0xdc
Pointer
(Int): dif
54. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers and Functions (Example 5.1)
54
20
Int: x
0xf1
10
Int: y
0xcc
30
Int: s
0xfd
10
Int: d
0xdc
20
Int: a
10
Int: b
0xfd
Pointer
(Int): sum
0xdc
Pointer
(Int): dif
55. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers and Functions (Example 5.1)
55
20
Int: x
0xf1
10
Int: y
0xcc
30
Int: s
0xfd
10
Int: d
0xdc
s: 30, d: 10
20
Int: a
10
Int: b
0xfd
Pointer
(Int): sum
0xdc
Pointer
(Int): dif
59. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers and Functions (Example 5.2)
59
x: 10, y: 20
10
Int: x
0xf1
20
Int: y
0xcc
&x
&y
10
Int: t
0xfc
0xf1
Pointer
(Int): a
0xcc
Pointer
(Int): b
60. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers and Functions (Example 5.2)
60
x: 10, y: 20
20
Int: x
0xf1
20
Int: y
0xcc
&x
&y
10
Int: t
0xfc
0xf1
Pointer
(Int): a
0xcc
Pointer
(Int): b
61. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers and Functions (Example 5.2)
61
x: 10, y: 20
20
Int: x
0xf1
10
Int: y
0xcc
&x
&y
10
Int: t
0xfc
0xf1
Pointer
(Int): a
0xcc
Pointer
(Int): b
62. Professor Joongheon Kim
https://joongheon.github.io
C Programming
Pointers and Functions (Example 5.2)
62
x: 10, y: 20
x: 20, y: 10
20
Int: x
0xf1
10
Int: y
0xcc
&x
&y
10
Int: t
0xfc
0xf1
Pointer
(Int): a
0xcc
Pointer
(Int): b