Lab Pointers
Lab Pointers
Objectives:
1. Introduction to Pointers
a. Declaring and Using Pointer Variables
b. Using the Indirection (Dereference) operator ‘*’
c. Using Address-Of Operator ‘&’
d. Pointers and Arrays
INTRODUCTION TO POINTERS
In the previous labs, we have not considered methods to control the amount of memory used in
a program. In particular, in all of the programs we have looked at so far, a certain amount of
memory is reserved for each declared variable at compilation time, and this memory is retained
for the variable as long as the program or block in which the variable is defined is active. In this
part of lab we introduce the notion of a pointer, which gives the programmer a greater level of
control over the way the program allocates and de-allocates memory during its execution.
DECLARING POINTERS
A pointer is just the memory address of a variable, so that a pointer variable is just a variable in
which we can store different memory addresses. Pointer variables are declared using a "*", and
have data types like the other variables we have seen. For example, the declaration
int *number_ptr; states that "number_ptr" is a pointer variable that can store addresses of
variables of data type "int".
Then the variable ‘rate’ is stored at a specific memory address and as an illustration, can be
depicted as follows:
In the above example:
In programming we can find this address by “&” operator. This operator is called address
operator.
Given a particular data type, such as "int", we can write assignment statements involving both
ordinary variables and pointer variables of this data type using the dereference operator "*" and
the (complementary) address-of operator "&". Roughly speaking, "*" means "the variable located
at the address", and "&" means "the address of the variable".
POINTER OPERATIONS
Pointer Operators
Operator Meaning
* Dereference (given a pointer, get the thing
referenced)
& Address of (given a thing, point to it)
EXAMPLE (POINTERS)
#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
#include<iostream>
using namespace std;
void square(int *n);
int main() OUTPUT
EXAMPLE (POINTERS-II)
{ int v[3]={2,4,6};
for (int i=0;i<3;i++)
{
cout<<v[i]<<endl;
}
square(v); for (int i=0;i<3;i++)
{ cout<<v[i]<<endl;
} }
void square(int*n)
{
cout<<"**********"<<endl; for (int
i=0;i<3;i++)
{
cout<<n<<endl;
cout<<*n<<endl; *n= *n * *n;
cout<<n<<endl;
cout<<*n<<endl; n=n+1;
cout<<"**********"<<endl;
}
}
• Suppose, pointer needs to point to the fourth element of an array, that is, hold address
of fourth array element in above case. Since ptr points to the third element in the above
example, ptr + 1 will point to the fourth element.
• You may think, ptr + 1 gives you the address of next byte to the ptr. But it's not correct.
• This is because pointer ptr is a pointer to an int and size of int is fixed for a operating
system (size of int is 4 byte of 64-bit operating system). Hence, the address between ptr
and ptr + 1 differs by 4 bytes.
• If pointer ptr was pointer to char then, the address between ptr and ptr + 1 would have
differed by 1 byte since size of a character is 1 byte.
LAB TASKS
LAB TASK NO 1: DEFINING POINTERS
Perform the following steps in a C++ program.
• Initialize num1, num2 and num3 with integers 3,4 and 6
• Initialize pointers variables ptr1, ptr2 and ptr3 to store address of num1, num2, and num3
respectively
• Perform following operations o *ptr1=*ptr2+*ptr3; o *ptr1++; o *ptr2--;
• Display output of num1, num2, num3 and addresses stored in the pointers ptr1, ptr2 and
ptr3 before and after the operations.
• Explain the difference in the outputs.
REQUIRED OUTPUT