CHAPTER SIX:
POINTERS
Computer Memory Cells
The memory of your computer can be imagined as a succession of
memory cells, each one of the minimal size that computers manage
(one byte).
These single-byte memory cells are numbered in a consecutive way, so
as, within any block of memory, every cell has the same number as the
previous one plus one.
This way, each cell can be easily located in the memory because it has
a unique address and all the memory cells follow a successive pattern.
For example, if we are looking for cell 1776 we know that it is going to
be right between cells 1775 and 1777
It is exactly one thousand cells after 1776 and exactly one thousand
cells before cell 1776.
2
Pointers:-
The variable that stores the reference to another variable is
called a pointer.
In other words, pointers are variables that hold an address
value of another variable.
Advantage of a pointer:-
Offering a very efficient method of accessing data
They provide efficient techniques for manipulating data in
arrays
They are used in functions for reference parameters
for dynamic allocation of memory. 3
Reference operator (&) and Deference operator (*)
The reference operator (&) returns the variable’s address.
The dereference operator (*) helps us get the value that
has been stored in a memory address.
For example:
If we have a variable given the name num, stored in the
address 0x234 and stored the value 28.
The reference operator (&) will return 0x234.
The dereference operator (*) will return 28.
Reference operator (&)
The address that locates a variable within memory is a reference
to that variable.
Reference to a variable can be obtained by preceding the
identifier of a variable with an ampersand sign (&), known as
reference operator, and which can be literally translated as
"address of".
Fragmented Example:
string food= "Pizza"; // A food variable of type string
cout << food; // Outputs the value of food (Pizza)
cout << &food; // Outputs the memory address of food
(0x6dfed4)
5
Example 1:Reference Operator
#include<iostream>
using namespace std;
int main()
{
int var1=10,var2=20,var3=30;
cout<<endl<<&var1<<endl<<&var2<<endl<<&var3;
return 0;
}
6
Dereference Operator (indirection operator) (*)
Pointers are said to "point to" the variable whose reference they store.
Using a pointer we can access the value stored in the variable which it
points to.
Value=*Ptr;//// read as: "Value equals to the value pointed by Ptr“
Ptr refers to the address location( say 1776)
*Ptr refers to the value stored at address 1776.
Example: The Int type pointer points only for the int type value
int var=10 int var=10
double *p = &var int *p = &var
7
Pointer Declaration Syntax
datatype *variable_name;
The datatype is the base type of the pointer which must be a valid
C++ data type.
The variable_name is should be the name of the pointer variable.
The asterisk used above for pointer declaration is similar to the
asterisk used to perform multiplication operations. It is the asterisk
that marks the variable as a pointer. Here is an example of valid
pointer declarations in C++:
int *x; // a pointer to integer
double *x; // a pointer to double
float *x; // a pointer to float
char *ch // a pointer to a character
string food ="Pizza"; // A food variable of type string
string* ptr =&food; // A pointer variable, with the name ptr, that stores
the address of food
cout << food << "\n"; // Output the value of food (Pizza)
cout << &food << "\n"; // Output the memory address of food (0x6dfed4)
// Output the memory address of food with the pointer (0x6dfed4)
cout << *ptr << "\n";
Example 2:Reference and Dereference
#include <iostream>
using namespace std;
int main()
{
int num = 1000;
int *ptr = #
cout << "Value of num:"<< *ptr <<endl;
cout<<"the adress is: "<<&ptr<<endl;
*ptr = 50;
cout << "New value of num: " << *ptr <<endl;
return 0;
}
Pointer example #3
#include<iostream>
using namespace std;
int main()
{
int num=16; // An integer variable.
int * ptr=# //
cout<<"Value stored in variable num:"<<num <<endl;
cout<<"Address of variable num:"<< ptr <<endl;
cout<<"Value in the variable:" <<*ptr <<endl;
cout<<"Address of variable num:"<< &num <<endl;
return 0;
}
Pointer Example:4
#include <iostream>
using namespace std;
int main()
{
int x = 27;
int *ip;
ip = &x;
cout << "Value of x is : "<<x<<endl;
cout<< "the size of int x is "<<sizeof(x)<<endl;
cout << "Value of ip is”<< ip<< endl;
cout << "Value of *ip is “<<*ip << endl;
return 0;
}
Example 5:
#include<iostream>
using namespace std;
void swap(int *,int *); // This is swap's prototype
int main()
{
int x = 5, y = 7;
cout << "\n x is now "<< x << " and y is now " << y << '\n';
swap(&x , &y); // calling swap with reference parameters
cout << "\n x is now "<< x << " and y is now " << y << '\n’;
}
void swap(int *a, int *b)
{ int temp;
temp = *a;
*a = *b;
*b = temp;
}
Structure and Array
In C++, you can use pointers #include<iostream>
to work with arrays. When using namespace std;
you declare a pointer to an int main() {
array, you can access the int arr[]={1, 2, 3, 4, 5};
elements of the array using int* ptr = arr;
pointer arithmetic. for (int i = 0; i < 5; i++) {
cout << "Element " << i << ": "
<< *ptr <<endl;
ptr++; }
return 0;}
Structures & Enumerations
In many occasions what we want to store are not mere
sequences of elements all of the same data type( as in
arrays), but sets of different elements with different data
types.
Structures offer a way of joining these heterogeneous
elements together to form complex structures.
15
A structure is a group of data elements grouped together
under one name.
These data elements, known as members, can have
different types and different lengths.
Structures are declared in C++ using the following syntax:
struct structure_name //struct is a keyword
{
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
}
object_names;
structure_name is a name for the structure type,
16
object_name can be a set of valid identifiers for objects that have the type
of this structure.
Within braces { } there is a list with the data members, each one is
specified with a type and a valid identifier as its name.
Example:
struct person
{
float height;
int eye_color;
char name[20]
};
person mother;person friend,spouse;
We can also declare the structure objects mother, father and spouse at the
moment we define the structure like this way:
struct stud1 {
float height;
int eye_color;
char name[20]
} mother, friend,spouse; 17
Struct
{
int myNum;
string myString;
}
myStruct1, myStruct2, myStruct3; // Multiple structure
variables separated with commas
18
Accessing structure Members
We use the dot operator (.) to access members of a
structure. We use this format to read, to write or assign
data to members of a structure. We can assign value to
the name of stud1 as follows:
stud1. name =”Abebe”;
stud1.eye_color=1;
Stud1.height=1.70;
Generally, structure members are treated just like other
variables.
19
20
Struct Example
#include <iostream>
#include <string>
using namespace std;
int main() {
struct {
string brand;
string model;
int year;}
myCar1, myCar2; // Put data into the first structure
myCar1.brand = "Toyota";
myCar1.model = "Vitz";
myCar1.year = 2010;
myCar2.brand = "Hyundai"; // Put data into the second structure
myCar2.model = "Atoz";
myCar2.year = 1999;
cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year
<< "\n";
cout << myCar2.brand << " " << myCar2.model << " " << myCar2.year
<< "\n";
return 0;}
Enumeration
In C++, enumeration, often referred to as "enum," is a
user-defined data type used to create a set of named
integer constants.
Enums are typically used to improve code readability and
maintainability by assigning meaningful names to integral
values. Here's how you can define and use enums in C+
+.
Example
#include <iostream>
using namespace std;
enum Color
{ RED, // 0
GREEN, // 1
BLUE, };
int main()
{ Color myColor=GREEN;
if (myColor== RED)
cout << "The color is red."<<endl;
else if (myColor==GREEN)
cout << "The color is green."<<endl;
else
cout << "The color is blue."<<endl;
return 0;}
1. What is Reference operator and
Dereference operator show by
example?
2. If a function doesn’t return a value,
how do
you declare the function?
3. Write a simple function program to
add and
subtract two numbers and show
function declaration
function calling and
function definition
Thank You for Your Time!
END OF THE COURSE: COMPUTER
PROGRAMMING
FACULTY OF ELECTRICAL ENGINEERING
Department of Electrical and Computer Eng.
Department of Biomedical Eng.