Docsity

Pointers and Dynamic Arrays in C++: Data Structures Lab, Lecture notes of Data Structures and Algorithms

Toggle sidebar
bg1
Data Structures | Level 2 | Lab 2
1 | P a g e
Pointers & Dynamic Array
1- Pointers
Pointer variable: A variable whose content is an address (that is, a memory address).
Program #1:
Let us consider the following statements:
int *ptr;
int x;
In these statements, ptr is a pointer variable of type int and x is a variable of a type int. Let
us assume that memory location 001DFACC is allocated for ptr and memory location
001DFAD8 is allocated for x.
Consider the following statements:
1. x = 5; 2. ptr = &x; 3. *ptr = 24;
Write C++ Program to show the values of the variables after the execution of each
statement.
#include <iostream> Memory Address
#include<string>
using namespace std; X 001DFAD8
void main()
{
int x =5, *ptr; ptr
ptr=&x; //stores the address of x in ptr 001DFACC
cout<<"The Value of X ="<<x<<endl;
cout<<"The Address of X ="<<&x<<endl;
cout<<"The Value of ptr ="<<ptr<<endl;
cout<<"The Value pointed by pointer ptr "<<*ptr<<endl;
cout<<"The Address of ptr ="<<&ptr<<endl;
*ptr=24;
cout<<"The Value of X after changes ="<<x<<endl;
//delete ptr;
}
Output:
5
&x
pf3

Partial preview of the text

Download Pointers and Dynamic Arrays in C++: Data Structures Lab and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Pointers & Dynamic Array

1 - Pointers

Pointer variable: A variable whose content is an address (that is, a memory address).

Program #1: Let us consider the following statements: int *ptr; int x; In these statements, ptr is a pointer variable of type int and x is a variable of a type int. Let us assume that memory location 001DFACC is allocated for ptr and memory location 001DFAD8 is allocated for x. Consider the following statements:

  1. x = 5; 2. ptr = &x; 3. *ptr = 24; Write C++ Program to show the values of the variables after the execution of each statement.

#include Memory Address #include using namespace std; X 001DFAD void main() { int x =5, ptr; ptr ptr=&x; //stores the address of x in ptr 001DFACC cout<<"The Value of X ="<<x<<endl; cout<<"The Address of X ="<<&x<<endl; cout<<"The Value of ptr ="<<ptr<<endl; cout<<"The Value pointed by pointer ptr "<<ptr<<endl; cout<<"The Address of ptr ="<<&ptr<<endl; *ptr=24; cout<<"The Value of X after changes ="<<x<<endl;

//delete ptr; }

Output:

&x

2 - Dynamic Array

There is a substantial difference between declaring a normal array and allocating dynamic memory for a block of memory using new. The most important difference is that the size of a regular array needs to be a constant expression , and thus its size has to be determined at the moment of designing the program, before it is run, whereas the dynamic memory allocation performed by new allows to assign memory during runtime using any variable value as size.

Every program has its own three memory segments, ie, code segment in which the compiled (object/machine) code resides, stack segment in which the local/temporary variables are stored and heap segment which is used for dynamic memory allocation.

Program #2: Implementation of Dynamic Array

#include #include using namespace std; void main() { int *arr, size;

cout<<"Enter Number of Elements \n"; cin>>size; arr=new int [size]; cout<<"Enter "<<size<<" Elements \n"; for (int i=0;i<size;i++) { cin>>arr[i]; } cout<<"You Entered \n"; for (int i=0;i<size;i++) { cout<<arr[i]<<endl; } delete[] arr;