0% found this document useful (0 votes)
10 views

Lab Pointers

The document provides an introduction to pointers in programming, detailing how to declare and use pointer variables, as well as the indirection and address-of operators. It explains pointer operations, their relationship with arrays, and includes examples and lab tasks for practical application. The lab tasks involve defining pointers, swapping variables using pointers, and adding arrays using pointers.

Uploaded by

abubakar01901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lab Pointers

The document provides an introduction to pointers in programming, detailing how to declare and use pointer variables, as well as the indirection and address-of operators. It explains pointer operations, their relationship with arrays, and includes examples and lab tasks for practical application. The lab tasks involve defining pointers, swapping variables using pointers, and adding arrays using pointers.

Uploaded by

abubakar01901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LAB: POINTERS - I

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".

For Example: int


rate = 100;

Then the variable ‘rate’ is stored at a specific memory address and as an illustration, can be
depicted as follows:
In the above example:

Address of variable “rate” = &rate = 1004

In programming we can find this address by “&” operator. This operator is called address
operator.

ADDRESS-OF 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".

We can write a small code for saving the address of an integer:

int *ptr_rate; int rate=100; ptr_rate=&rate; // with this line


“ptr_rate” will be equal to 1004

POINTER OPERATIONS

Following are the operation of pointers

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

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;
}
OUTPUT

#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;
}
}

POINTERS AND ARRAYS

• Pointers are the variables that hold address. Not


only can pointers store address of a single
variable, it can also store address of cells of an array.

int* ptr; int arr[5]; ptr = &arr[2]; // &a[2] is the address of


third element of a[5].

• 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.

EXAMPLE (POINTERS AND ARRAYS)

#include <iostream> OUTPUT


using namespace std; int main() {
float arr[5]; float *ptr;
cout << "Displaying address using arrays: " << endl;
for (int i = 0; i < 5; ++i)
{
cout << "&arr[" << i << "] = " << &arr[i] << endl;
} ptr = &arr[0];
cout<<"\nDisplaying address using pointers: "<< endl;
for (int i = 0; i < 5; ++i)
{
cout << "ptr + " << i << " = "<< ptr + i << endl;
}
}

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

LAB TASK NO 2: SWAPPING VARIABLES USING POINTERS


Write a C++ program that swaps values of two float type variables A and B. For swapping you can
use third variable temp to store values temporary. All accesses to the variables (A, B, and temp)
should be made through pointer notation only. In the end, print the values of variables (A, B) on
the screen. Note that during input and output the variables must be accessed through pointers
only.
REQUIRED OUTPUT

LAB TASK NO 3: ADD ARRAYS USING POINTERS


Suppose you have a main() with three local arrays, all the same size (5 elements) and type (float).
Initialize the first two arrays using pointers from the user. Write a function called addarrays() that
accepts the addresses of the three arrays as arguments; adds the contents of the first two arrays
together, element by element; and places the results in the third array. A fourth argument to this
function can carry the size of the arrays (if required). Use pointers only.
REQUIRED OUTPUT

You might also like