0% found this document useful (0 votes)
3 views

lesson 4

The document provides an overview of a lecture on data structures and programming in C++, focusing on pointers. It explains what pointers are, their advantages, and how to use them, including pointer operators and initialization. Additionally, it includes examples demonstrating the use of pointers in C++ programming.

Uploaded by

Da Vy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

lesson 4

The document provides an overview of a lecture on data structures and programming in C++, focusing on pointers. It explains what pointers are, their advantages, and how to use them, including pointer operators and initialization. Additionally, it includes examples demonstrating the use of pointers in C++ programming.

Uploaded by

Da Vy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecture overview

Data structure and ❑ Overall lectures

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

Remark: Pointer is a variable that store address of


another variable of the same data type.
C++
2

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)

▪ Examples ▪ The address is a numerical number (expressed in hexadecimal)

▪ An integer value consumes 4 bytes of memory

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

▪ Example: point to variable


name
▪ If num is a variable, then &num gives the address of that variable
An example of a pointer variable
pointing to a normal variable
5 6

Pointer Vs. Normal variable Advantages of using pointer?


❑ Remark ❑ Some main advantages
▪ A normal variable is used to store value, while a pointer variable is used to 1. Use less memory
store address (reference) of another variable ▪ Dynamic memory allocation
▪ Pointers are representation of addresses
2. Program runs faster
▪ We can have a pointer to any variable type ▪ Increase execution speed and reduce execution time
3. Efficient when work with array, structure, list, stack, queue, …
4. Provide another way to access array element
5. Instead of copying data, pointer just point to an existing data
6. A function can return more than one value by passing via
function argument
7 8
Pointer Operator Pointer Declaration
❑ What? ❑ Syntax
▪ Two operators when work with pointer
▪ Address operator (reference operator)
▪ It uses &
▪ It returns memory address
▪ Indirection operator (deference operator or value operator)
▪ It uses *
▪ It returns value

9 10

Pointer Initialization Access to Pointer Variable


❑ Syntax ❑ Syntax

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

Pointer and Array Pointer and Array


❑ Remark

▪ Suppose we have variables


▪ Var arr[10]: integer
▪ Var *p:integer

▪ 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

▪ &: to get address of any variable

▪ *: to get value at the address that the point stores

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

Example 1: Using C++ Example 2: Using C++

▪ Suppose we have program as follows


#include<iostream>
▪ Suppose we have program as follow
using namespace std; Output
int main(){
int *pc, c;
#include<iostream>
c=5;
using namespace std;
int main(){ cout<<“Address of c: ”<<&c<<endl;
cout<<“Value of c: ”<<c<<endl;
int num=10; pc = &c;
int *ptr; cout<<“Address that pc holds: ”<<pc<<endl;
ptr = &num; cout<<“Value of address that pc holds: ”<<*pc<<endl;
c = 11;
cout<<“Address that pc holds: ”<<pc<<endl;
cout<<“num=”<<num<<endl; cout<<“Value of address that pc holds: ”<<*pc<<endl;
*pc = 2;
cout<<“Address of variable num is: ”<<&num<<endl;
cout<<“Address of c: ”<<&c<<endl;
cout<<“Value in variable pointer ptr is: ”<<ptr<<endl; cout<<“Value of c: ”<<c<<endl;
cout<<“*ptr=”<<*ptr<<endl; //Value in that address }

} 19 20
Pointer Examples

Write program in C++ using pointer


Remark: Pointer is a variable that store address of
another variable of the same data type.
Q&A

22

You might also like