You are on page 1of 51

Introduction to Programming

Compiled by
G. Pachshenko
Week 12
Lecture 12
By the end of Week 13 you must do

1. 70 tasks (acmp) of your variant


2. Defend tasks to your professor
Pointers
Pointers
Address-of Operator (&)
The dereference operator *
Pointer Comparisons
Incrementing a Pointer
Null pointer
Function Pointers
What Are Pointers?

A pointer is a variable whose value is the


address of another variable.
The pointer declaration looks like this:

<variable_type> *<name>;
Note: The asterisk (*) sign in the declaration of
the pointer and only means that it is a pointer .
int *p;

OR,

int* p;
Example 1
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
Example 2
int *p;
int *p2;
double *pd;
To pointers p and p2 can be assigned the
addresses of type int, but it can not be assigned
the addresses of other types.

Similarly, to the pointer pd we can only assign


the addresses of type double.
Definition. What does Address-of Operator (&) mean?

An address-of operator is a mechanism within


C++ that returns the memory address of a
variable. These addresses returned by the
address-of operator are known as pointers,
because they "point" to the variable in memory.

The address-of operator is a unary operator


represented by an ampersand (&). It is also
known as an address operator.
Address-of Operator (&)

Address operators commonly serve two purposes: 

• To conduct parameter passing by reference, such as by


name (see a lecture about functions)
• To establish pointer values. Address-of operators point
to the location in the memory because the value of
the pointer
is the memory address/location
where the data item resides in memory.
Example 3
include <iostream>
using namespace std;
int main ()
{
int var1;
char var2[10];
cout << "Address of var1 variable: ";
cout << &var1 << endl;
cout << "Address of var2 variable: ";
cout << &var2 << endl;
return 0;
}
Result
Address of var1 variable: 0xbfebd5c0
Address of var2 variable: 0xbfebd5b6
& is the reference operator and can be read as
“address of”.
The dereference operator. *
 The sign * is called the dereference operator.
If the dereference operator is used you will get
the “value pointed by” a pointer.
Note: The asterisk (*) sign in the declaration of
the pointer does not mean “value pointed by”, it
only means that it is a pointer. It should not be
confused with the dereference operator. They are
simply two different things represented with the
same sign.
* is the dereference operator and can be read as
“value pointed by”.
Example 4
int x;
int *p;

// * is used in the declaration:


// p is a pointer to an integer, since (after dereferencing),
// *p is an integer

x = 0; // now x == 0
p = &x; // & takes the address of x
// now p == &x, so *p == x
*p = 1;
// equivalent to x = 1, since *p == x
// now *p == 1 and *p == x, so x == 1
Example 5
#include <iostream>
using namespace std;
 
int main()
{
int var = 123; 
int *ptrvar = &var; 
cout << "&var    = " << &var << endl;
cout << "ptrvar  = " << ptrvar << endl; // address of var is ptrvar

cout << "var     = " << var << endl; //


cout << "*ptrvar = " << *ptrvar << endl;  // to print out var using the
dereference operator

system("pause");
  return 0; }
Result:
&var = 0x22ff08
ptrvar = 0x22ff08
var = 123
*ptrvar = 123
Pointer Comparisons
Pointers may be compared by using relational
operators, such as ==, <, and >.
#include "stdafx.h" Example 6
#include <iostream>
using namespace std;
 
int main(int argc, char* argv[])
{
  int var1 = 123; 
   int var2 = 99; 
   int *ptrvar1 = &var1; 
    int *ptrvar2 = &var2; 
    cout << "var1    = " << var1 << endl;
   cout << "var2    = " << var2 << endl;
 cout << "ptrvar1 = " << ptrvar1 << endl;
  cout << "ptrvar2 = " << ptrvar2 << endl;
 if (ptrvar1 > ptrvar2) 
cout << "ptrvar1 > ptrvar2" << endl;
  if (*ptrvar1 > *ptrvar2) 
  cout << "*ptrvar1 > *ptrvar2" << endl;
 system("pause");
 return 0;
}
Result:
var1 = 123
var2 = 99
ptrvar1 = 0x22ff04
ptrvar2 = 0x22ff00
ptrvar1 > ptrvar2
*ptrvar1 > *ptrvar2
Incrementing a Pointer:

We prefer using a pointer in our program instead


of an array because the variable pointer can be
incremented, unlike the array name which cannot
be incremented because it is a constant pointer.
The following program increments the variable
pointer to access each succeeding element of the
array.

ptr++
#include <iostream> Example 7
using namespace std;
const int MAX = 3;
int main ()
{ int var[MAX] = {10, 100, 200};
int *ptr; // let us have array address in pointer.
ptr = &var[0];

for (int i = 0; i < MAX; i++)


{
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl; // point to the next location
ptr++;
}

return 0; }
Result:
Address of var[0] = 0xbfa088b0
Value of var[0] = 10
Address of var[1] = 0xbfa088b4
Value of var[1] = 100
Address of var[2] = 0xbfa088b8
Value of var[2] = 200
Decrementing a Pointer:

ptr--
Example 8
#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{ int var[MAX] = {10, 100, 200};
int *ptr; // let us have address of the last element in pointer.
ptr = &var[MAX-1];

for (int i = MAX; i > 0; i--)


{ cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl; // point to the previous location
ptr--; }

return 0;
}
Null pointer
A null pointer is a regular pointer.
It only indicates that it is not pointing to a valid
memory address or reference.

Null pointers point to “nowhere”.


#include <iostream>
using namespace std;
int main ()
{
int *ptr = NULL;
cout << "The value of ptr is " << ptr ;
return 0;
}
When the above code is compiled and executed,
it produces the following result:
The value of ptr is 0
On most of the operating systems, programs are
not permitted to access memory at address 0
because that memory is reserved by the
operating system.
However, the memory address 0 has special
significance; it signals that the pointer is not
intended to point to an accessible memory
location. But by convention, if a pointer contains
the null (zero) value, it is assumed to point to
nothing.
if(ptr) // succeeds if p is not null
if(!ptr) // succeeds if p is null
A function can return a pointer.
Instead of a regular value or even a reference, a
function can return a pointer. You can start to
specify this by typing the * operator on the left
side of the function's name.

double *GetSalary() { }
Then, use the body of the function to define it.
Before the closing curly bracket of the function,
remember to return a pointer to the return value.

double *GetSalary()
{
double salary = 26.48;
double *HourlySalary = &salary;
return HourlySalary;
}
Because a pointer by defining is a reference to
the address where a variable resides, when a
function is defined as returning a pointer, you can
also return a reference to the appropriate type.

double *GetSalary()
{
double salary = 26.48;
return &salary;
}
After defining the function, you can call it.
Remember that the asterisk is used to get the
value of a pointer.

This means that, when calling the function, if


you want to access its value, make sure you
include the asterisk on the left side of the name
of the function.
1
#include <iostream>
using namespace std;
double & GetWeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours;
}
2
double *GetSalary()
{
double salary = 26.48;
double *HourlySalary = &salary;
return HourlySalary;
}
3
int main()
{ double hours = GetWeeklyHours();
double salary = *GetSalary();
cout << "Weekly Hours: " << hours << endl;
cout << "Hourly Salary: " << salary << endl;
double WeeklySalary = hours * salary;
cout << "Weekly Salary: " << WeeklySalary <<
endl;
return 0; }
This would produce:

Weekly Hours: 46.5


Hourly Salary: 26.48
Weekly Salary: 1231.32
Function Pointers
A pointer is a variable that holds the
address of another variable.
Function pointers are similar, except
that instead of pointing to variables,
they point to functions!
• // pointer_onfunc
• 
• #include <iostream>
• using namespace std;
• int nod(int, int ); // prototype of the function
• int main()
•{
•     int (*ptrnod) (int, int); // pointer to function
•     ptrnod=nod; // address of the function to a pointer ptrnod
•     int a, b;
•     cout << "Enter first number: ";
•     cin >> a;
•     cout << "Enter second number: ";
•     cin >> b;
•     cout << "NOD = " << ptrnod(a, b) << endl; // to call the function using the pointer
•     system("pause");
•     return 0;
•}
• int nod(int number1, int number2) // recursive function
•{
•     if ( number2 == 0 ) //
•         return number1;
•     return nod(number2, number1 % number2); //
•}

You might also like