Professional Documents
Culture Documents
Compiled by
G. Pachshenko
Week 12
Lecture 12
By the end of Week 13 you must do
<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.
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
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:
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];
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];
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.
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.