Oop Lab 2 PDF
Oop Lab 2 PDF
Oop Lab 2 PDF
C++ Pointers
In this lab, you'll learn everything about pointers. You'll learn how values are stored in the
computer and how to access them using pointers.
Pointers are powerful features of C++ that differentiates it from other programming
languages like Java and Python.
Pointers are used in C++ program to access the memory and manipulate the address.
Address in C++
To understand pointers, you should first know how data is stored on the computer.
Each variable you create in your program is assigned a location in the computer's
memory. The value the variable stores is actually stored in the location assigned.
To know where the data is stored, C++ has an & operator. The & (reference) operator
gives you the address occupied by a variable.
Output
0x7fff5fbff8ac
Note: You may not get the same result on your system.
Pointers 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.
Pointers variables are variables that points to a specific address in the memory pointed
by another variable.
OR,
int* p;
The statement above defines a pointer variable p. It holds the memory address
The asterisk is a dereference operator which means pointer to.
Here, pointer p is a pointer to int, i.e., it is pointing to an integer value in the memory
address.
To get the value stored in the memory address, we use the dereference operator (*).
For example: If a number variable is stored in the memory address 0x123, and it
contains a value 5.
The reference (&) operator gives the value 0x123, while the dereference (*) operator
gives the value 5.
Note: The (*) sign used in the declaration of C++ pointer is not the dereference pointer.
It is just a similar notation that creates a pointer.
Output
Address of c: 0x7fff5fbff80c
Value of c: 5
Address of c: 0x7fff5fbff80c
Value of c: 2
int c, *pc;
pc=c; /* Wrong! pc is address whereas, c is not an address. */
*pc=&c; /* Wrong! *pc is the value pointed by address whereas, &c is an address.
*/
*pc=c; /* Correct! *pc is the value pointed by address and, c is also a value. */
int main()
{
int a = 1, b = 2;
swap(&a, &b);
cout << a << endl;
cout << b << endl;
return 0;
}
Output
a = 1
b = 2
int* ptr;
int a[5];
C++ Program to insert and display data entered by using pointer notation.
int main() {
float arr[5];
Output
Lab Task:
Question 1: Declare three integers x, y and sum and three pointers xPtr, yPtr, sumPtr.
Point three pointers to their respective variable.
Take input in x and y using xPtr and yPtr. Do not use direct references i.e. x and y
integers
Add x and y and save the result in sum. Do not use direct references i.e. x, y and sum
integers
Print addition’s result. For example, if user entered x = 5 and y = 9 your program should
print: 5 + 9 = 14. Do not use direct references i.e. x, y and sum integers
Note: You have to do all the processing using pointers i.e. indirect references to
variables.
Question 3: Rewrite the following loop so it uses pointer notation (with the indirection
operator) instead of subscript notation.
for (int i = 0; i < 10; i++)
cout << array[i] << endl;
Question 4: Write a function which will take pointer and display the number on screen.
Take number from user and print it on screen using that function.
Question 5: Write a C++ program to accept five integer values and print the elements
of the array in reverse order using a pointer.
Question 6: Make an array of 6 elements, find the largest number from the array and
show the value and address of that element using pointer