Slide5 Pointer

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 70

Introduction to Programming

Pointers
Phạm Nguyên Khang
Department of Computer Science
pnkhang@cit.ctu.edu.vn

5/2021
Contents
• Pointers
• Pointers & Arrays
• Pointers & Function
• Dynamic Memory Allocation
• Null, Void and Double Pointers
• Practice

2
Pointers

3
Pointers
• Variable & Address
• Every variable (var) has its address (&var) in
memory
– &: reference operator
– You have seen it in scanf function

4
Pointers

5
Pointers
• Results

6
Pointers

7
Pointers
• Results

• Notice:
– You may obtain different value of address while using
this code.
– In above source code, value 5 is stored in the memory
location 0x7fff5c242bb8. var is just the name given to
that location.

8
Pointers
• Pointer Variables
– special variable that stores just the address of
another variable. It is called Pointer variable or,
simply, a pointer.

p p is a pointer variable of type int.

9
Pointers
• Reference operator (&) and Dereference
operator (*)
– &:reference operator. It gives you the address of a
variable.
– *: dereference operator. It gives you the value
from the address.
• Note:
– The * sign when declaring a pointer is not a
dereference operator.
10
11
12
Pointers
• Results

13
14
Pointers
• Assign a pointer to another
q

p a

15
Pointers
• Assign a pointer to another
q

p a

16
Pointers
• Assign a pointer to another
q

p a

17
Pointers & Arrays

18
Pointers & Arrays
• Arrays are closely related to pointers in C
programming
• A pointer variable takes different addresses as
value whereas, in case of array it is fixed.

19
Pointers & Arrays

20
Pointers & Arrays
• Results

• Try to with an array of int or float or double to


see how addresses change!

21
Pointers & Arrays
• Relation between Arrays and Pointers

22
Pointers & Arrays
• Name of the array always points to address of
the first element of an array.
• For example: arr and &arr[0] points to the
address of the first element.

23
Pointers & Arrays

• You can declare an array and can use pointer


to alter the data of an array!

24
25
Pointers & Arrays
• Result

26
Pointers & Arrays
• Relation between Arrays and Pointers

27
Pointers & Arrays
• Operators on Pointers

28
Pointers & Arrays
• Operators on Pointers
– p is a pointer of type T
– p++ or p = p + 1 move the pointer an offset of
sizeof(T)

1 byte 1 byte 1 byte 1 byte 1 byte 1 byte 1 byte 1 byte 1 byte

29
Pointers & Arrays
• Operators on Pointers
– p is a pointer of type T
– p++ or p = p + 1 move the pointer an offset of
sizeof(T)

1 byte 1 byte 1 byte 1 byte 1 byte 1 byte 1 byte 1 byte 1 byte

30
Pointers & Functions

31
Pointers & Functions
• Write a program to swap two numbers

32
33
34
Pointers & Functions
• Write a program to swap two numbers using
function

35
36
37
Why doesn’t it work as we want ?

38
a b

temp

num1 num2

5 10
39
a b

5 10

temp

copy

num1 num2

5 10
40
a b

5 10

temp

num1 num2

5 10
41
Pointers & Functions
• swap cannot modify of num1 and num2
because it receive only value of num1 and
num2.
• We cannot access a local variable outside its
scope (by using its name)

• Solution: using address of the variable instead


of its name
– Pass address of num1 and num2 to swap.

42
a b

temp

num1 num2

5 10
43
a b

temp

refer to

num1 num2

5 10
44
Pointers & Functions
• Parameter passing: copy value of arguments
to parameters
• Cannot access a variable outside its scope
using its name
• Always access any variable using its address.

45
Pointers & Functions
• Pointers as return value of functions

Address must be a
“valid” address

46
47
48
Dynamic Memory Allocation

49
Dynamic Memory Allocation
• Size of array must have been known at
compile time.
• Size of the array can be insufficient or more
than required.

50
Dynamic Memory Allocation
• Write a program to enter n float numbers,
store them for later use.

51
Dynamic Memory Allocation

52
Dynamic Memory Allocation
• What if the user enter
–n=3? Waste of memory
– Or n = 100 ? Insufficient

è Dynamic memory allocation


– allows your program to obtain more memory
space while running, or to release it if it's not
required.

53
54
Dynamic Memory Allocation
Functio Use of function
n
malloc() Allocates requested size of bytes and returns
a pointer first byte of allocated space
calloc() Allocates space for an array elements,
initializes to zero and then returns a pointer
to memory
free() Deallocate the previously allocated space
realloc() Change the size of previously allocated space

55
Dynamic Memory Allocation
• Example:

56
Null, Void and Double Pointers
• Null pointer
– Pointer point to nothing, contains 0
• Void pointer (void*)
– General Purpose Pointer
– It does not have any data type associated with it
– It can store address of any type of variable
– A void pointer is a C convention for a raw address

57
Void Pointers

58
59
Double Pointers
• Pointer to another pointer

• Double Pointer stores the


address of the Pointer Variable

60
Practice
1. Write a program to realize followings:
- Declare a integer variable named balance.
- Declare a pointer to integer named address.
- Store the address of balance to address.
- Store 5 to balance.
- Print on the screen: value of balance, address of balance
- Print on the screen: value of address, content of memory
cell where address points to.
- Assign 10 to memory cell where address points to.
- Print on the screen: value of balance, content (value)of
memory cell where address points to.

61
Practice
1. Complete this program to enter 5 integers,
stores them in array A, then prints them on the
screen using pointer instead of [] operator.

62
Practice
3. Consider the code in the following page.
a. Add your code in functions foo1 and foo2
b. Compile and run, observe and explain obtained
results.

63
64
Practice
4. Write a function ADD with void as returned
type, 3 integer parameters: a, b, c. This function
compute a + b and store the result to c. Write
the main() function to use this function.

65
Practice
5. Write a program to do followings:
– Declare a integer variable n.
– Declare a pointer to float p.
– Have the user enter the value of n (scanf, n < 10)
– Allocate n floats (use malloc()) to p.
– Assign 1/1, 1/2, 1/3, …, 1/n to elements of p (use
pointer movement)
– Print n elements to the screen using [] operator (point
is used as array)
– Deallocate p (use free())

66
Practice
6. Write a function void UPDATE(n, p) to allocate n
integer numbers for p (p is a pointer).
Write main() function to do the followings:
– Declare a pointer to integer q.
– Declare a integer variable n.
– Have the user enter value of n (scanf(), n ~ 1000)
– call UPDATE function to allocate memory for q.
– assign 2, 4, 6, 8, … to elements of q.
– Compute sum of elements of q, print it to the screen
– Deallocate p.

67
Practice
7. Write a program in C to find the largest
element (integers) using Dynamic Memory
Allocation.
Require: Enter n numbers and store them in a
dynamic array.

68
Practice
8. Write a program in C to print the elements of a
dynamic array in reverse order using dynamic
memory allocation and pointers.

9. Write a function int *dec2bin(int n) to convert a


number in decimal into binary.
Hint:
– Declare a pointer in dec2bin: int * p;
– Allocate memory for p (use malloc())
– Store binary digits of n in p
– return p.
69
Practice
10. Write a program to use an array of pointers
instead of 2D array.
A pB
5 2 3 5 2 3
4 7 1
4 7 1

70

You might also like