Professional Documents
Culture Documents
Chapter 8-Pointer
Chapter 8-Pointer
programming II
Chapter 8- Pointer
Prepared by:
Dr. VALY Dona and Mr. NOU Sotheany, Mr. BOU Channa
1
Lecture overview
❑ Overall lectures
C++
2
Outline
▪ What is pointer?
▪ Examples
3
Introduction
❑ Computer Memory
4
Introduction
❑ Computer Memory
▪ Each variable we create in the program has a location in the computer’s memory
▪ To know where the data of normal variable is stored, we use operator &
▪ & gives the address occupied by a variable
▪ Example:
▪ If num is a variable, then &num gives the address of that variable
5
Introduction
❑ What is pointer?
▪ A pointer is a variable that holds the memory address of another variable of the
same type.
▪ Pointers are used to access the memory address and values at that address.
value
point to variable
name
7
Advantages of using pointer?
❑ Some main advantages
1. Use less memory
▪ Dynamic memory allocation
2. Program runs faster
▪ 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
8
Pointer Operator
❑ What?
▪ 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
Pointer Declaration
❑ Syntax
10
Pointer Initialization
❑ Syntax
11
Access to Pointer Variable
❑ Syntax
12
Example 1
❑ Not using pointer
15
Pointer and Array
❑ Remark
▪ 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
▪ write(*p) 16
NOTE
❑ Reference (&) Vs. Deference (*) operator
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
Q&A
18
Example 1: Using C++
#include<iostream>
using namespace std;
int main(){
int num=10;
int *ptr;
ptr = #
cout<<“num=”<<num<<endl;
cout<<“&num=”<<&num<<endl;
cout<<“ptr=”<<ptr<<endl;
cout<<“*ptr=”<<*ptr<<endl;
}
19
Example 2: Using C++
#include<iostream>
▪ Suppose we have program as follow
using namespace std; Output
int main(){
int *pc, c;
c=5;
cout<<“Address of c: ”<<&c<<endl;
cout<<“Value of c: ”<<c<<endl;
pc = &c;
cout<<“Address that pc holds: ”<<pc<<endl;
cout<<“Value of address that pc holds: ”<<*pc<<endl;
c = 11;
cout<<“Address that pc holds: ”<<pc<<endl;
cout<<“Value of address that pc holds: ”<<*pc<<endl;
*pc = 2;
cout<<“Address of c: ”<<&c<<endl;
cout<<“Value of c: ”<<c<<endl;
}
20
Size of Data Type
C++ program
21
Q&A
22
Practice
❑ Exercises
1. Write a C++ program that declares and initializes any values to a variable of type float, integer
and string. The program then display the value and address (in hexadecimal form) for each
variable.
2. Write a C++ program to get two integer numbers from a user then swap the values of those two
variables and display them on screen.
3. Create a subprogram to swap the values of the four parameters. The first two parameters
exchange each other. The last two parameters exchange each other.
4. Create a function to solve quadratic equation. The function takes 6 parameters. The functions
solve roots then store in parameters. Prototype of this function is defined as below:
void solveEquation(float a, float b, float c, float *x1, float *x2, float *delta)
23
24
25
26
NOTE
27
Remarks
❑ Using pointer with single star (*) and double star (**)
#include<iostream> ▪ The variable i is an int, and set to the value 42.
using namespace std; ▪ pi is a pointer-to-int, and set to the address of i.
int main(){ ▪ ppi is a pointer-to-pointer-to-int, and set to the address of pi.
int i = 50;
int *pi = &i;
int **ppi = π
cout<<*pi<<endl; **ppi *p i
cout<<**ppi<<endl;
}
https://www.youtube.com/watch?v=8ViFuvTezIc 28
https://www.quora.com/What-do-the-two-asterisks-signify-in-C++-when-passing-int-**array
Remarks
❑ Using pointer with single star in parameter of function
#include<iostream>
With this function declaration getA, there are two possibilities:
using namespace std;
1. This function can takes an array of integers as a parameter
void getA(int *pi ){ 2. This function can also take an integer, passed by reference, as a
//do something parameter
count<<*pi<<endl;
}
int main(){
int i = 42;
int *p=&i;
int arr1[]={1,2,7};
getA(arr1);
getA(&i);
getA(*p);
} 29
Understanding header file
❑ Definition
✓ A header file is a file that stores the list of functions or variables created by a user.
✓ With header file, it allows programmers to pack their codes as functions in a separated external file
which allow them to store functions in just a single file and reuse those functions when needed.
✓ Header file has .h or .hpp extension. We create a header file then put our functions there.
✓ Header file can be included to a normal .cpp source code file by using:
30
Creating a header file
❑ Example
Calling to function
created in header file
#include<iostream>
using namespace std;
int main(){
int num1=10;
int num2=20;
int num3=30;
cout<<&num1<<endl;
cout<<&num2<<endl;
cout<<&num3<<endl;
}
33
Example 1
❑ Address operator
▪ Suppose we have program as follow
#include<iostream>
#include<iostream> using namespace std;
using namespace std; int main(){
int main(){ int num=10;
int num=10; int *p;
cout<<“Value of number: ”<<num<<endl; p=#
cout<<“Address of number: ”<<&num<<endl; cout<<“Address that p stores: ”<<p<<endl;
} cout<<“Value at that address : ”<<*p<<endl;
}
34
Pointer variables
❑ Syntax
▪ We can assign and de-assign any space in memory using Pointer variables
35
Pointer variables
❑ More examples of creating pointer variables
▪ A pointer to an integer value
▪ int *p1;
▪ A pointer to a double value
▪ double *p2;
▪ Another valid syntax
▪ int* p1;
▪ int * p2;
▪ int *p3, *p4;
▪ Invalid syntax
▪ int p1*;
36
Example of Value Operator
❑ Value operator
▪ Suppose we have program as follows
#include<iostream>
using namespace std;
int main(){
int num=10;
int *ptr;
ptr = #
cout<<“num=”<<num<<endl;
cout<<“&num=”<<&num<<endl;
cout<<“ptr=”<<ptr<<endl;
cout<<“*ptr=”<<*ptr<<endl;
} 37
NOTE
❑ Reference (&) Vs. Deference (*) operator
▪ Example:
▪ If an integer variable, say n, is stored in memory address 0xf1bd23, and it contains a
value of 5
▪ Then
▪ Reference operator (&n) gives the value of 0xf1bd23
▪ Deference operator (*n) gives the value of 5
38
Example
#include<iostream>
▪ Suppose we have program as follow
using namespace std; Output
int main(){
int *pc, c;
c=5;
cout<<“Address of c: ”<<&c<<endl;
cout<<“Value of c: ”<<c<<endl;
pc = &c;
cout<<“Address that pc holds: ”<<pc<<endl;
cout<<“Value of address that pc holds: ”<<*pc<<endl;
c = 11;
cout<<“Address that pc holds: ”<<pc<<endl;
cout<<“Value of address that pc holds: ”<<*pc<<endl;
*pc = 2;
cout<<“Address of c: ”<<&c<<endl;
cout<<“Value of c: ”<<c<<endl;
}
39
Example: Display address using cout and printf
#include<iostream>
#include<stdio.h>
Output
using namespace std;
int main(){
int n=5;
int *p;
p=&n;
cout<<n<<" "<<&n<<endl;
cout<<p<<" "<<*p<<endl;
printf("\nn=%d, address of n=%p", n, &n);
printf("\np stores %d which is the address of
n,\n value of that address is: %d\n", p, *p);
}
40
Using pointer to swap two values via a function
parameter
41
Homework
❑ Reading: Linked List
2. Why linked list is important? Give some differences between array and linked list?
*NOTE:
42
Note
❑ Constant pointer Vs. Pointer to a constant
▪ Constant pointer
▪ Address stored in pointer is constant
▪ That address can not be changed (always points to the same address)
▪ The value stored at that address can be changed
▪ Pointer to a constant
▪ Pointer is pointing to a constant
▪ Address stored in pointer can be changed
▪ Pointer always point to a constant value
#include<iostream>
using namespace std;
int main(){
int num=10;
cout<<“Value of number: ”<<num<<endl;
cout<<“Address of number: ”<<&num<<endl;
}
44
C Programming
❑ Code syntax
▪ All C program statement begins inside a function called main()
▪ The main function is always called when the program first executes
▪ To access the standard functions that comes with compiler, a header with the
▪ #include <stdio.h> need to be included on top
▪ Structure of code
#include <stdio.h>
#include <stdio.h> int main(){
printf(“Hello World!”);
int main(){
printf(“Welcome to C Programming.”);
… Write your code here … }
}
Example of code in C programming