Lab Manual 12 - Pointers
Lab Manual 12 - Pointers
Lab- 12 Manual
Lab Instructor: Riaz Ahmad
Department of Computer Science
Email: [email protected]
Lab 1: Pointers
Lab Rubrics
Objective(s):
To Understand about:
1. Able to use Pointers
2. Able to use pointers in function and structure.
Introduction
Pointers are powerful features of C++ that differentiates it from other
programming languages like Java and Python.
Pointers are used in C++ program to access the memory and manipulate the address.
type *var-name;
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of
the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use
for multiplication. However, in this statement the asterisk is being used to designate a variable as a
pointer.
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is
the same, a long hexadecimal number that represents a memory address. The only difference
between pointers of different data types is the data type of the variable or constant that the
pointer points to.
#include <iostream>
using namespace std;
int main ()
{
int var = 20; // actual variable declaration.
int *ip; // pointer variable
ip = &var; // store address of var in pointer variable
cout << "Value of var variable: ";
cout << var << endl;
// print the address stored in ip pointer variable
cout << "Address stored in ip variable: ";
cout << ip << endl;
// access the value at the address available in pointer
cout << "Value of *ip variable: ";
cout << *ip << endl;
return 0;
}
When the above code is compiled and executed, it produces result something as follows:
Value of var variable: 20
Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20
Address in C++
To understand pointers, you should first know how data is stored on the computer.
Each variable you create in your program is assigned a location in the computer's
memory. The value the variable stores is actually stored in the location assigned. To
know where the data is stored, C++ has an & operator. The & (reference) operator
gives you the address occupied by a variable. If var is a variable then, &var gives the
address of that variable.
int main()
{
int var1 = 3;
int var2 = 24;
int var3 = 17;
cout << &var1 << endl;
cout << &var2 << endl;
cout << &var3 << endl;
}
Output
0x7fff5fbff8ac
0x7fff5fbff8a8
0x7fff5fbff8a4
Note: You may not get the same result on your system.
The 0x in the beginning represents the address is in hexadecimal form.
Notice that first address differs from second by 4-bytes and second address differs
from third by 4-bytes.
This is because the size of integer (variable of type int) is 4 bytes in 64-bit system.
Pointers Variables
C++ gives you the power to manipulate the data in the computer's memory directly.
You can assign and de-assign any space in the memory as you wish. This is done
using Pointer variables.
Pointers variables are variables that points to a specific address in the memory
pointed by another variable.
int *p;
OR,
int* p;
c = 5;
cout << "Address of c (&c): " << &c << endl; cout << "Value of c
(c): " << c << endl << endl;
c = 11; // The content inside memory address &c is changed from 5 to 11. cout <<
"Address pointer pc holds (pc): " << pc << endl;
cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;
*pc = 2;
cout << "Address of c (&c): " << &c << endl; cout << "Value of c
(c): " << c << endl << endl;
return 0;
}
Output
Example:
#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;}
Explanation of program
When c = 5; the value 5 is stored in the address of variable c - 0x7fff5fbff8c.
When pc = &c; the pointer pc holds the address of c - 0x7fff5fbff8c, and the expression (dereference
operator) *pc outputs the value stored in that address, 5.
When c = 11; since the address pointer pc holds is the same as c - 0x7fff5fbff8c, change in the value of
c is also reflected when the expression *pc is executed, which now outputs 11.
When *pc = 2; it changes the content of the address stored by pc - 0x7fff5fbff8c. This is changed from
11 to 2. So, when we print the value of c, the value is 2 as well.
Common mistakes when working with pointers
Suppose, you want pointer pc to point to the address of c. Then,
int c, *pc;
pc=c; /* Wrong! pc is address whereas, c is not an address. */
*pc=&c; /* Wrong! *pc is the value pointed by address whereas, %amp;c is an address.
*/ pc=&c; /* Correct! pc is an address and, %amp;pc is also an address. */
*pc=c; /* Correct! *pc is the value pointed by address and, c is also a value. */
int main()
{
void centimize(double&); //prototype double
var = 10.0; //var has value of 10 inches
cout << “var = “ << var << “ inches” << endl;
centimize(var); //change var to centimeters
cout << “var = “ << var << “ centimeters” << endl;
return 0;
}
Here we want to convert a variable var in main() from inches to centimeters. We pass the
variable by reference to the function centimize(). (Remember that the & following the data type
double in the prototype for this function indicates that the argument is passed by reference.)
The centimize() function multiplies the original variable by 2.54. Notice how the function refers
to the variable. It simply uses the argument name v; v and var are different names for the same
thing. Once it has converted var to centimeters, main() displays the result.
The next example, PASSPTR, shows an equivalent situation when pointers are used:
// passptr.cpp
// arguments passed by pointer
#include <iostream>
using namespace std;
void centimize(double* ptrd)
{
*ptrd *= 2.54; //*ptrd is the same as var
}
int main()
{
void centimize(double*); //prototype
double var = 10.0; //var has value of 10 inches
cout << “var = “ << var << “ inches” << endl;
centimize(&var); //change var to centimeters
cout << “var = “ << var << “ centimeters” << endl;
return 0;
}
FIGURE
Home Work: