0% found this document useful (0 votes)
21 views31 pages

Main PDF Lesson 5

The document discusses pointers and dynamic arrays in C++. It defines pointers and how they store the memory address of a variable. It also explains how to declare and initialize pointers. The document then covers dynamic arrays which are created using pointers and new operator, and how to declare, initialize and delete dynamic arrays.

Uploaded by

onecarloscruz111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views31 pages

Main PDF Lesson 5

The document discusses pointers and dynamic arrays in C++. It defines pointers and how they store the memory address of a variable. It also explains how to declare and initialize pointers. The document then covers dynamic arrays which are created using pointers and new operator, and how to declare, initialize and delete dynamic arrays.

Uploaded by

onecarloscruz111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

MODULE 5

POINTERS AND DYNAMIC ARRAY


LESSON #1
Pointers
Upon completion of this subtopic, you will be able to:
• Learn about the pointers and its syntax definition.
• Demonstrate how to declare a pointer just like
declaring an ordinary variable of a certain type
• is the memory address of a variable.
Syntax:
Type_Name *Pointer_1, *Pointer_2,…,*Pojnter_n;
Declaring a pointer is just like declaring an ordinary
variable of a certain type. For example:
int *p1, *p2, v1, v2;
p1 and p2 are pointers
v1 and v2 are variable
Declaration Description
int *p; pointer that points to a variable
of type int
float *p; pointer that points to a variable
of type float
double *p; pointer that points to a variable
of type double
MyType *p; pointer that points to a variable
of type MyType Class
• The * operator in front of a pointer variable produces
the variable to which it points. When used this way,
the * operator is called the dereferencing operator.

• The & in front of an ordinary variable produces the


address of that variable; that is, it produces a pointer
that points to the variable. The & symbol is used to
determine the address of a variable. The & operator is
called the address-of operator.
#include <iostream>
using namespace std;
int main () {
int var1;
Output:
char var2[10];
cout << "Address of var1 variable: ";
cout << &var1 << endl;
cout << "Address of var2 variable: ";
cout << &var2 << endl;
return 0;
}
#include <iostream> Output:
using namespace std;
int main()
{ int *p , v;
v = 0;
p = &v;
*p = 42;
cout << v << endl;
cout << *p << endl;
return 0;
}
It is always a good practice to assign the pointer NULL to a
pointer variable in case you do not have exact address to be
assigned.

This is done at the time of variable declaration. A pointer that


is assigned NULL is called a null pointer.

The NULL pointer is a constant with a value of zero defined


in several standard libraries, including iostream
#include <iostream>
using namespace std;
int main () {
int *ptr = NULL;
cout << "The value of ptr is " << ptr ;
return 0;
}
Output:
#include <iostream>
using namespace std;
int main(){
int *p; //Pointer declaration
int arr[]={1, 2, 3, 4, 5, 6}; //Array declaration
p = arr; //Assignment
for(int i=0; i<6;i++){
cout<<*p<< " "; //++ moves to next int position
p++;
} Output:
return 0;
}
• Since a pointer can be used to refer to a variable, your
program can manipulate variables even if the variables
have no identifiers to name them.
• The new operator creates a new dynamic variable of a
specified type and returns a pointer that points to this new
variable.
This new, nameless variable can be referred to as *p1
(that is, as the variable pointed by p1)
If the possible output would be like this
Output:
• It is a special area of memory that is reserved for
dynamically allocated variables.
• Any new dynamic variable created by a program
consumes some of the memory in the freestore.
• If there was insufficient available memory to create the
new variable, the new operator will return a special
value NULL.
• The delete operator eliminates a dynamic variable and
returns the memory that the dynamic variable occupied to
the freestore manager so that the memory can be reused.
• When you apply delete to a pointer variable, the dynamic
variable to which it is pointing is destroyed.
• At that point, the value of the pointer variable is undefined,
which means that you do not know where it is pointing.
• A pointer variable pointing to the dynamic variable that was
destroyed and becomes undefined is called dangling
pointers.
https://www.w3schools.com/cpp/cpp_pointers.asp
https://www.tutorialspoint.com/cplusplus/cpp_pointers.htm
https://www.geeksforgeeks.org/pointers-c-examples/
https://www.programiz.com/cpp-programming/pointers
https://www.javatpoint.com/cpp-pointers
LESSON #2
Dynamic Array
Upon completion of this subtopic, you will be able to:
• Understand what is typedef
• Understand what is a dynamic array
• Demonstrate how to declare and use dynamic arrays
and pointer variables
You can assign a name to a type definition and then use the
type name to declare variables. This is done with the
keyword typedef.

Syntax:
typedef Known_Type_Definition New_Type_Name;

Example:
typedef int* IntPtr;
The type name IntPtr can then be used to declare
pointers to dynamic variables of type int, as in the
following:

IntPtr pointer1, pointer2;


• Dynamic arrays are created using the new operator.
Since array variables are pointer variables, you can
use the new operator to create dynamic variables
• You can treat these dynamic array variables as if they
were ordinary arrays
• For example, the following creates a dynamic array
variable with ten array elements of type double:

typedef double* DoublePtr;


DoublePtr d;
d = new double[10];
Define a pointer type: Define a type for pointers to variables
of the same type as the elements of the array. For example, if
the dynamic array is an array of double, you might use the
following:
typedef double* DoubleArrayPtr;
Declare a pointer variable: Declare a pointer variable of this
defined type. The pointer variable will point to the dynamic
array in memory and will serve as the name of the dynamic
array.
DoubleArrayPtr a;
Call new: Create a dynamic array using the new operator:

a = new double[array_size];

The size of the dynamic array is given in square brackets as


in the above example
Use like an ordinary array: The pointer variable, such as a,
is used just like an ordinary array.

For example, the indexed variables are written in the usual


way: a[0], a[1], and so forth. The pointer variable should not
have any other pointer value assigned to it, but should be
used like an array variable
Call delete []: When your program is finished with the
dynamic variable, use delete and empty square brackets
along with the pointer variable to eliminate the dynamic array
and return the storage that it occupies to the freestore for
reuse. For example:

delete [] a;
#include <iostream>
using namespace std;
int main(){
int size;
cout << "Enter size of an array "; Output:
cin >> size;
int *array = new int[size];
cout << "Input " << size << endl;
for (int i = 0; i < size ;i++)
{ cin >> array[i]; }
cout << "The contents of the array ";
for (int i = 0; i < size ;i++)
{ cout << array[i] << " "; }
delete [] array;
return 0;
}
https://www.geeksforgeeks.org/how-do-dynamic-arrays-work/
https://www.programiz.com/cpp-programming/pointers-arrays

You might also like