You are on page 1of 13

Programming Fundamentals using C

Slots 11-12
Pointers
Programming Fundamentals using C

3- What is a Pointer?
address value
 A pointer is a variable, which contains the
80000 32
address of a memory location of another
variable p2: 80000
 If one variable contains the address of
another variable, the first variable is said to 400 7
point to the second variable
p1: 400
 A pointer provides an indirect method of accessing
the value of a data item
 Pointers can point to variables of other fundamental data types
like int, char, or double or data aggregates like arrays or
structures

Pointers 2
Programming Fundamentals using C

3- What is a Pointer?

*p

8823045

10 29 52
x y z
8823087 8823090 8823100

Pointers 3
Programming Fundamentals using C

5- Where are pointers used?


• Some situations where pointers can be used are:
– To modify outside arguments of a function
– To return more than one value from a function (It will
be introduced in slots 1824)
– To pass array and strings more conveniently from one
function to another (It will be introduced in slots
1824)
– To manipulate arrays easily by moving pointers to them
instead of moving the arrays itself (It will be introduced
in slots 1824)
– To allocated memory and access it (Direct memory
allocation) (It will be introduced at the bottom of this
lesson)
Pointers 4
Programming Fundamentals using C

Variable Characteristics
Type name value MEMORY

15
6684364
int var1 = 15;
6684365
var 1
6684366
6684367

...

Pointers 5
Programming Fundamentals using C

Pointer Variable
MEMORY
dataType *name;

15
Syntax: 6684364

Ex: 6684365
var 1
int var1=15; 6684366
6684367
int *var2;

...

… null var 2

Pointers 6
Programming Fundamentals using C

6- Pointer Operators
How to Operator MEMORY

15
6684364
Get address of a & 6684365
variable and 6684366
var 1
assign it to a 6684367
pointer …
...
… 6684364 var 2
Ex: …
int var1=15; …
int *var2; …
var2= &var1; …

Pointers 7
Programming Fundamentals using C

6- Pointer Operators
How to Operator MEMORY

15
6684364
Access indirectly *
value of a data 6684365
var 1
through it’s 6684366
pointer 6684367

...

Ex: … 6684364 var 2

int var1=15,tmp; …

15
int *var2; …

var2=&var1; …
tmp
//int *var2=&var1; …

tmp=*var2;// tmp=15 …

Pointers 8
Programming Fundamentals using C

6- Pointer Operators
MEMORY

15
10
Ex: 6684364

int var1=15,tmp; 6684365


var 1
6684366
int *var2;
6684367
var2=&var1; …
//int *var2=&var1; ...
*var2= 10; … 6684364 var 2





Pointers 9
Programming Fundamentals using C

Example

Q&A

Pointers 10
Programming Fundamentals using C

Example

Gọi hàm hoán đổi, bằng truyền địa chỉ

int *a=&x; int *b=&y

Pointers 11
Programming Fundamentals using C

Pointer Operators… Walkthrough


100 n=7  54
*pn = 2*(*pm) + m*n;
96 m=6  -30
Value at 100 = 2*(value at 96) + m *n
92 pn=100
Value at 100 = 2*6 + 6 *7
88 pm= 96
Value at 100 = 12 + 42 = 54

*pm += 3*m - (*pn);


Value at 96 += 3*6 – value at 100
Value at 96 += 3*6 – 54
Value at 96 += 18 – 54
Value at 96 += (-36)
Value at 96 = 6+ (-36) = -30

Pointers 12
Programming Fundamentals using C

Walkthroughs: Do yourself

int n=7,m=8; int n=7, m=8;


int* p1= &n, *p2=&m; int* p1= &n, *p2=&m;
*p1 +=12-m+ (*p2); *p1 +=5 + 3*(*p2) –n ;
*p2 = m + n- 2*(*p1); *p2 = 5*(*p1) – 4*m + 2*n;
printf(“%d”, m+n); What are values of m and n?
What is the output?
Pointers 13

You might also like