OOP Lab 2 (Pointers)
OOP Lab 2 (Pointers)
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.
Here, 5 is assigned to the variable var. And, the address of var is assigned to the pointVar
pointer with the code pointVar = &var.
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.
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.
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].
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.
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.