CP-LAB-10
CP-LAB-10
Pointers in C++
Objectives:
In this lab students will learn:
• Memory concept of variables, pointers and how to use variable identifiers and pointers
to refer to the variable.
• Pointer variable declarations and initialization.
• Direct and indirect referencing a variable using the pointer operators.
• Using * and address (&) operators.
Equipment required:
• Dev-C++/Eclipse/Visual Studio installed in PC/Windows
DISCUSSION
1. Pre-Lab
Pointers
Pointers are powerful features of C++ that differentiates it from other programming languages
like Java and Python. Pointers are used in C++ to access the memory and manipulate the
address. When declaring a variable, it is located at a specific location in memory, the memory
address. The task of locating variables is automatically performed by the operating system
during runtime. In some cases, we need to know the address where the variable is being
stored during runtime. Variable which stores a reference to another variable is called a pointer.
We can directly access the value stored in the variable using a pointer which points to it.
Address in C++
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.
Example
In the example given below, 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.
Computer Programming Lab Prepared by: Engr. M. Farooq Khan
Pointer 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. Pointer variables are variables that points to a specific address in the memory
pointed by another variable.
Syntax:
1. Pointer Declaration:
Syntax: Pointer_datatype *Pointer_name;
Example: int *Ptr1; //pointer of data type int, and name Ptr1
double *Ptr2; //pointer of data type double, and name Ptr2
2. Pointer initialization:
Syntax: Pointer_name=&variable_name;
Example: int *Ptr1, var; //variable named var and pointer named Ptr1 is initialized.
Ptr1=&var; Ptr1 holds the address of variable var.
Note: Pointer with a star “*ptr” can hold any value, while pointer without star “ptr” can hold address
Example OUTPUT
int main() { ;
int *pc, c;
c = 5;
cout<<"Address of c (&c): "<<&c
<<endl;
cout<<"Value of c (c): "<< c
<<endl<<endl;
A pointer to a pointer
A pointer to a pointer is a form of multiple indirections or a chain of pointers. Normally, a pointer
contains the address of a variable. When we define a pointer to a pointer, the first pointer
contains the address of the second pointer, which points to the location that contains the actual
value as shown below.
A variable that is a pointer to a pointer must be declared as such. This is done by placing an
additional asterisk in front of its name. For example, following is the declaration to declare a
pointer to a pointer of type int as
END