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

OOP Lab 2 (Pointers)

The document provides an overview of pointers in C++, including their declaration, assignment, and usage with variables and arrays. It outlines tasks for programming assignments that involve creating pointers, manipulating values, and performing operations such as finding sums and swapping values. The document emphasizes practical coding exercises to reinforce understanding of pointers and their applications in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

OOP Lab 2 (Pointers)

The document provides an overview of pointers in C++, including their declaration, assignment, and usage with variables and arrays. It outlines tasks for programming assignments that involve creating pointers, manipulating values, and performing operations such as finding sums and swapping values. The document emphasizes practical coding exercises to reinforce understanding of pointers and their applications in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Submit one zipped folder named as your rollNumber that contains the required .

cpp files
and images of paper work.

Pointers

In C++, pointers are variables that store the memory addresses of other variables. Here is how
we can declare pointers. Here, we have declared a pointer pointVar of the int type.

Assigning Addresses to Pointers

Here, 5 is assigned to the variable var. And, the address of var is assigned to the pointVar
pointer with the code pointVar = &var.

Graphical Representation of Pointers


Get the Value from the Address Using Pointers

To get the value pointed by a pointer, we use the * operator.

In the above code, the address of var is assigned to pointVar. We have used the *pointVar to get
the value stored in that address. When * is used with pointers, it's called the dereference
operator. It operates on a pointer and gives the value pointed by the address stored in the
pointer. That is, *pointVar = var.

Changing Value Pointed by Pointer

If pointVar points to the address of var, we can change the value of var by using *pointVar. For
example,

Here, pointVar and &var have the same address, the value of var will also be changed when
*pointVar is changed.

C++ Pointers and Arrays

In C++, Pointers are variables that hold addresses of other variables. Not only can a pointer
store the address of a single variable, it can also store the address of cells of an array. Consider
this example:
Here, ptr is a pointer variable while arr is an int array. The code ptr = arr; stores the address of
the first element of the array in variable ptr. Notice that we have used arr instead of &arr[0]. This
is because both are the same.
The addresses for the rest of the array elements are given by &arr[1], &arr[2], &arr[3], and
&arr[4].

Point to Every Array Elements

Similarly, we can access the elements using the single pointer.

Suppose if we have initialized ptr = &arr[2]; then


Task 1

Write a program that asks the user to enter integers as inputs to be stored in the variables a and
b respectively. Also create two integer pointers named ptrA and ptrB. Assign the addresses of a
and b to ptrA and ptrB respectively. Display the values and addresses of a and b using ptrA and
ptrB.

Sample Output (addresses will be different in your case)

Task 2

& can be used as reference operator and address of operator. Write the output of the following
code and check your output by retyping the following code on the compiler. Assume that
address of a = 0x16b1fb068
Task 3

Write the output of the following code and check your output by retyping the following code on
the compiler. Assume that address of a = 0x16b1fb068 and ptr = 0x16b1d3060

Task 4

Write the output of the following code and check your output by retyping the following code on
the compiler. Assume that starting address of arr = 0x16d117050
Task 5

Write the output of the following code and check your output by retyping the following code on
the compiler. In case of error, mention the line number

Task 6

Write the output of the following code and check your output by retyping the following code on
the compiler. In case of error, mention the line number

Task 7

Write the output of the following code and check your output by retyping the following code on
the compiler. In case of error, mention the line number
Task 8

Write the output of the following code and check your output by retyping the following code on
the compiler. In case of error, mention the line number

Task 9

Create a function that takes five pointers as arguments to find the sum of 5 numbers using
pointers.

Task 10

Create a C++ program to swap the values of two variables. Arguments must be passed by
reference.

Task 11

Create a C++ program to find the largest number from the array of 7 numbers using pointer.
Subscript notation not allowed.

You might also like