Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism

Arrays and Pointers

C++ Object Oriented Programming

Write a C++ program to explain pointer arithmetic.

Arrays and Pointers

• Pointers are very useful for accessing the elements of an array. Using pointer we can directly access the element at particular location.

C++ Program

/***********************************************************************

Program uses the pointers and arrays with the help of pointer the contents of the array can be displayed.

**********************************************************************/

#include<iostream>

#include<cstdlib>

using namespace std;

void main()

{

int x[10],i;

clrscr();

cout<<"\n Enter the numbers five numbers \n";

for(i=0;i<5;i++)

cin>>x[i];

cout<<"\n You have entered ";

for(i=0;i<5;i++)

cout<<"\nAt address "<<(x+i)<<" the value is "<<*(x+i);

getch();

}

Output

Enter the numbers five numbers

11

22

33

44

55

You have entered

At address 0x8f6affe2 the value is 11

At address 0x8f6affe4 the value is 22

At address 0x8f6affe6 the value is 33

At address 0x8f6affe8 the value is 44

At address 0x8f6affea the value is 55


The x+i means base address of the array x plus the offset. For example if I want to know the value at x[5], it will be (x+5) = x[5]. The *(x+i) indicates the value stored at the x[i]th address.


1. Pointer Arithmetic

In C++, using pointers some arithmetic operations can be performed. These operations are ‒

• We can use increment (++) or decrement (‒ ‒) operator using pointers.

• We can add or subtract an integer to a pointer variable.

/************************************************************************

Program for using a pointer arithmetic.

***********************************************************************/

#include<iostream>

#include<cstdlib>

using namespace std;

#define size 10

void main(void)

{

int x[size],n;

int *k;

clrscr();

cout<<"\n How many elements are there in an array?\n";

cin>>n;

cout<<"\n Enter the elements in an array\n";

for(int i=0;i<n;i++).

      cin>>x[i];

k=&x[0];

cout<<"\n Accessing element(base address): "<<*k;

k++;

cout<<"\n Accessing element(k++): "<<*k;

k++;

cout<<"\n Accessing element(k++): "<<*k;

STAMU vit wonder at war's

k‒‒;

cout<<"\n Accessing element (k‒‒): "<<*k;

k=k+3;

cout<<"\n Accessing element(k+2): "<<*k;

getch();

}

Output

How many elements are there in an array?

5

Enter the elements in an array

10

20

30

40

50

Accessing element(base address): 10

Accessing element(k++): 20

Accessing element (k++): 30

Accessing element(k): 20

Accessing element(k+2): 50

In above program we have taken an array of integer type. Hence each element occupies two bytes of memory.


Fig. 4.4.1 Size of char, int and float type of pointer variables

 

Review Question

1.Write a C++ program to explain pointer arithmetic.   

 

Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Arrays and Pointers


Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation