Oop Lab 2 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Object Oriented Programing

Target: Pointers and Arrays

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.

If var is a variable then, &var gives the address of that variable.


Example 1: Address in C++
1. int main()
2. {
3. int var1 = 3;
4. cout << &var1 << endl;
5. }

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.

How to declare a pointer?


int *p;

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.

Reference operator (&) and Deference operator (*)


Reference operator (&) as discussed above gives the address of a variable.

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.

Example: C++ Pointers


C++ Program to demonstrate the working of pointer.
1. int main()
2. {
3. int *pc, c;
4.
5. c = 5;
6. cout << "Address of c: " << &c << endl;
7. cout << "Value of c: " << c << endl;
8.
9. pc = &c; // Pointer pc holds the memory address of variable c
10. cout << "Address that pointer pc holds "<< pc << endl;
11. cout << "Content of the address pointer pc holds " << *pc << endl;
12.
13. c = 11; // The content inside memory address &c is changed from 5 to
14. 11.
15. cout << "Address pointer pc holds " << pc << endl;
16. cout << "Content of the address pointer pc holds: " << *pc << endl;
17.
18. *pc = 2;
19. cout << "Address of c: " << &c << endl;
20. cout << "Value of c: " << c << endl;
21.
22. return 0;
23. }

Output
Address of c: 0x7fff5fbff80c
Value of c: 5

Address that pointer pc holds: 0x7fff5fbff80c


Content of the address pointer pc holds: 5

Address pointer pc holds: 0x7fff5fbff80c


Content of the address pointer pc holds: 11

Address of c: 0x7fff5fbff80c
Value of c: 2

Common mistakes when working with pointers


Suppose you want pointer pc to point to the address of c. Then,

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 an address and, &pc is also an address. */

*pc=c; /* Correct! *pc is the value pointed by address and, c is also a value. */

In both cases, pointer pc is not pointing to the address of c.

C++ Call by Reference: Using pointers


This method used is called passing by value because the actual value is passed. There
is another way of passing an argument to a function where the actual value of the
argument is not passed. Instead, only the reference to that value is passed.

Example: Passing by reference using pointers

int main()
{
int a = 1, b = 2;
swap(&a, &b);
cout << a << endl;
cout << b << endl;
return 0;
}

void swap(int* n1, int* n2)


{
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}

Output

a = 1
b = 2

C++ 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.

Consider this example:

int* ptr;

int a[5];

ptr = &a[2]; // &a[2] is the address of third element of a[5].

Since ptr points to the third element.

C++ Program to insert and display data entered by using pointer notation.

int main() {
float arr[5];

// Inserting data using pointer notation


cout << "Enter 5 numbers: ";
for (int i = 0; i < 5; ++i) {
cin >> *(arr + i) ;
}

// Displaying data using pointer notation


cout << "Displaying data: " << endl;
for (int i = 0; i < 5; ++i) {
cout << *(arr + i) << endl ;
}
return 0;
}

Output

Enter 5 numbers: 2.5


3.5
4.5
5
2
Displaying data:
2.5
3.5
4.5
5
2

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 2: Write a program to find the factorial of a number using pointers

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

You might also like