0% found this document useful (0 votes)
5 views10 pages

C++ Chapter 10 (L 24)

Uploaded by

heinzzaw215z
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)
5 views10 pages

C++ Chapter 10 (L 24)

Uploaded by

heinzzaw215z
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/ 10

University of Computer Studies

(Mandalay)

Chapter 10
Pointers

Presented By: Dr. May Phyo Thu


Lecturer
Faculty of Computer Science
Outline
▪ Addresses and Pointers​
▪ The Address-of Operator (&)​
▪ Pointer Variables (Syntax, Value, Accessing the Variable Pointed to)

Object-Oriented Programming in C++, 4th Edition 2


Objectives

▪ To explain the fundamental concepts of pointers​

▪ To understand the syntax and semantics of pointers​

Object-Oriented Programming in C++, 4th Edition 3


Fundamental Concepts of Pointer​
▪ Pointers store memory addresses
▪ Reference variable can also be used to get the memory address of a variable.
▪ The address of a memory location of a variable through the reference operator “
& ”​
▪ The value of pointer variables can be accessed indirectly by using dereferencing
operator “ * ”​

Object-Oriented Programming in C++, 4th Edition 4


Addresses and Pointers ​
Addresses​
▪ A computer memory location has an
address and holds a content.
▪ Every byte in the computer’s memory has
an address.
▪ The address is a numerical number
(hexadecimal) which is hard for
programmers to use directly.

Figure 1 : Memory Addresses

Object-Oriented Programming in C++, 4th Edition 5


The Addresses -of Operators & ​
▪ The address operator(&) returns the address of memory where a variable is
located. It is called reference operator.
▪ The address of variable can be obtained by preceding the name of a variable with
an ampersand sign(&). data variable name
Example
11 value
int data ;
data = 11; 0x0000ff01 variable’s address
cout << “Variable’s name : data” <<endl;
Variable’s name : data​
cout << “Value of Data : ”<< data <<endl;
Value of data: 11​
cout << “Address of Data : ”<< &data <<endl; Address of data: 0x0000ff01​

Object-Oriented Programming in C++, 4th Edition 6


Addresses and Pointers(Cont’d) ​
Pointer is a variable which stores the int var;
address of another variable. Which is int *ptr;
used to access the memory and
var = 10;
manipulate the address.
ptr = 10;
C++ provides two pointer operators
ptr = var;
▪ Address of Operator &
ptr = &var;
▪ Indirection Operator *​
cout <<“\n value of var=” << var;
value of var = 10​ cout <<“\n value of pointer=”<< ptr;
value of pointer = 0001​
cout <<“\n point value of ptr=” << *ptr;
point value of ptr = 10​
address of var = 0001​ cout <<“\n address of var=” << &var;
address of ptr = 0009​ cout <<“\n address of ptr=” << &ptr;
Object-Oriented Programming in C++, 4th Edition 7
Pointers Variables​
▪ char * cptr; //pointer to char
▪ int * iptr; //pointer to int
▪ float* fptr; //pointer to float
▪ Distance* distptr;//pointer to Distance (user defined data type)

Pointer
char* ptr1, * ptr2, *ptr3; Declaration
int *ptr4, *ptr5;

A pointer variable can hold the address of any variable of the correct type; it’s a
receptacle awaiting an address.
Object-Oriented Programming in C++, 4th Edition 8
Accessing the Variable Pointed To
(Example)​
#include <iostream>
using namespace std;
int main()
{
int var1, var2; // declarations of two variables ​
int* ptr: // declaration of pointer variable​
var1 = 11; // value 11 is assigned to var1 ​
var2 = 22; // value 22 is assigned to var2 ​
ptr = &var1; //pointer points to var1​
cout <<“var1:”<< *ptr << endl; //print contents of pointer (11)
ptr = &var2; //pointer points to var2 ​
var1:11
cout <<“var2:”<< *ptr << endl; //print contents of pointer (22)
var2:22​
return 0; ​
}​
Object-Oriented Programming in C++, 4th Edition 9
Next Lecture

Let’s start Topic (Pointers and Arrays)

Outlook Gmail contact: [email protected]

Object-Oriented Programming in C++, 4th Edition 10

You might also like