lesson 4
lesson 4
programming II 1.
2.
Introduction to algorithm
Basic data types and statements
7.
8.
Recursive
File IO
9. Pointers
3. Control structures and Loop
10. Linked Lists
4. Array
11. Stacks and Queues
Pointer 5. Data structure 12. Sorting algorithms
Write program in C++ using pointer 6. Sub-programs 13. Trees
Outline Introduction
❑ Computer Memory
▪ What is pointer? ▪ To understand pointers, you should have knowledge about address in
computer memory
▪ What are the advantages of using pointer?
▪ How to use pointer ▪ A computer memory location has an address and holds a content (value)
3 4
Introduction Introduction
❑ Computer Memory ❑ What is pointer?
▪ Each variable we create in the program has a location in the computer’s memory ▪ A pointer is a variable that holds the memory address of another variable of the
same type.
▪ The value of the variable is stored in the assigned location ▪ Pointers are used to access the memory address and values at that address.
▪ To know where the data of normal variable is stored, we use operator &
▪ & gives the address occupied by a variable pointer* variable address
value
9 10
11 12
Example 1 Example 2:
❑ Not using pointer ❑ Using pointer
What are the values of a and b here? a is 1, b is 2 13 What are the values of a and b here? a is 2, b is 1 14
▪ Array name arr represents the address of the first elements of this array (&arr[0])
▪ We can say
▪ p = arr; // p point to the first element (arr[0]) in the array
▪ When a pointer points to an array, the value of the pointer is the first array element
15 ▪ write(*p) 16
NOTE
❑ Reference (&) Vs. Deference (*) operator
Q&A
Example:
▪ If an integer variable, say n, is stored in memory address 0xf1bd23,
and n contains a value of 5.
Then:
Reference operator &n gives the value of 0xf1bd23
Deference operator *n gives the value of 5
17 18
} 19 20
Pointer Examples
22