C++ Chapter 6-pointers about pointers in programming
firehiwot8
7 views
13 slides
Nov 02, 2025
Slide 1 of 13
1
2
3
4
5
6
7
8
9
10
11
12
13
About This Presentation
about pointers in programming
Size: 268.4 KB
Language: en
Added: Nov 02, 2025
Slides: 13 pages
Slide Content
Pointer In C++
11/02/25 1
Pointer
•A pointer is a variable that used to store a
memory address of other variable.
11/02/25 2
Pointer operator
1. Indirection (dereference) operator ‘*’
2. Unary (reference) operator ‘&’.
1. Reference (unary) & operator
The address that locates a variable within memory is
what we call a reference to that variable.
The unary operator returns the address of the memory
where a variable is located.
Any variable preside by & is read as “Address of …“
Example
11/02/25 3
X = 25;
v = X;
P = & x;
11/02/25 4
X
1775 1776 1777
25 1776
25
Pv
&
The values contained in each variable after the execution of
the above code segment, are shown in the following diagram
Variable X contains both address and actual value 25
Variable v contains actual value of X
A Variable that stores the reference of other variable is called
Pointer: P stores address of X so, P is pointer
2. DEREFERENCE ( INDIRECTION ) * OPERATOR
A Variable that stores the address of other variable is called Pointer.
Using a pointer we can directly access the value stored in the
variable which it points to.
Dereference operator will literally translated as <Value pointed
by…>
Example:- This will reads as
<val equal to value pointed by p>
11/02/25 5
val = *P
11/02/25 6
val would take a value 25 since P is 1776 and <the
value pointed by 1776> is 25.
Notice the difference b/n & and *:
& is reference and read as "address of”
* is the dereference and read as <"value pointed by“>
val = *P
Class activity
•Look at the diagram below which of comparison statement will
return true?
A. (P ==25) B. (P==1776)
C. (val == 1776) D. (val == 25)
E. (*p ==25)
11/02/25 7
pointer declaration
The general form of declaring pointer is:-
Data type is the base type of the variable pointed by the
pointer and variable_name is the name of the pointer
variable .
For example,
There are three declaration of pointers each of them
intended to different data type
11/02/25 8
Data type * variable_name;
int * number;
char * character;
float * greatnumber;
Example
11/02/25 9
#include <iostream>
using namespace std;
int main () {
int firstvalue, secondvalue;
int * mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout << "firstvalue is " << firstvalue endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}
Pointer initialization
When declaring pointers we may want to explicitly specify which
variable point to:
We can initialize pointer into either
Reference or address of other variable
Null or Zero(0) Pointer
11/02/25 10
int number;
int *tommy = &number;
int number;
int *tommy;
tommy = &number;
int number;
int *tommy;
*tommy = &number;
Wrong
int *tommy ;
tommy = 0;
tommy = Null;
Null Pointer
Initialization
NULL POINTER:
A null pointer is a regular pointer of any
pointer type which has a special value that
indicates it is not pointing to any valid
reference or memory address.
11/02/25 11
Class activity
•Look at the code and write
description of each line
statements Line-1 To Line-3
•For Line-4 and Line-5 write
plaintext equivalent of their
meaning
11/02/25 12
Line-1:
int * P ;
Line-2: int c=100;
Line-3 : int x;
Line-4 : P=&c;
Line-5 : x=* P;
?
Example
11/02/25 13
#include<iostream>
using namespace std;
int main ()
{
int *p, c=200, x;
p=&c;
x=*p;
cout << " Address of the memory of p: " << p << endl;
cout << " The contents of the pointer p : " << *p << endl;
cout << " The contents of the variable x : " << x << endl;
system(" pause " );
return (0);}