0% found this document useful (0 votes)
6 views7 pages

Pointers in CPP

The document provides an overview of pointers in C++, explaining their definition, usage, and syntax. It covers various applications such as dynamic memory allocation, pointer arithmetic, and the concept of void pointers. Additionally, it discusses invalid and null pointers, along with the advantages of using pointers in programming.

Uploaded by

sidrarahman014
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)
6 views7 pages

Pointers in CPP

The document provides an overview of pointers in C++, explaining their definition, usage, and syntax. It covers various applications such as dynamic memory allocation, pointer arithmetic, and the concept of void pointers. Additionally, it discusses invalid and null pointers, along with the advantages of using pointers in programming.

Uploaded by

sidrarahman014
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/ 7

C++ Programming

Topperworld.in

Pointers

⚫ The pointer in C++ language is a variable, it is also known as locator or


indicator that points to an address of a value.
⚫ The symbol of an address is represented by a pointer. In addition to creating
and modifying dynamic data structures, they allow programs to emulate call-
by-reference.
⚫ One of the principal applications of pointers is iterating through the
components of arrays or other data structures. The pointer variable that
refers to the same data type as the variable you're dealing with has the
address of that variable set to it (such as an int or string).

Syntax:
datatype *var_name;
int *ptr; // ptr can point to an address which holds int data

❖ How to use a pointer?


1. Establish a pointer variable.
2. employing the unary operator (&), which yields the address of the
variable, to assign a pointer to a variable's address.
3. Using the unary operator (*), which gives the variable's value at the
address provided by its argument, one can access the value stored in an
address.

©Topperworld
C++ Programming

Since the data type knows how many bytes the information is held in, we
associate it with a reference. The size of the data type to which a pointer points
is added when we increment a pointer.

❖ Usage of pointer
There are many usage of pointers in C++ language.

1) Dynamic memory allocation


In c language, we can dynamically allocate memory using malloc() and calloc()
functions where pointer is used.

2) Arrays, Functions and Structures


Pointers in c language are widely used in arrays, functions and structures. It
reduces the code and improves the performance.

❖ Symbols used in pointer

Symbol Name Description

& (ampersand sign) Address operator Determine the address of a variable.

∗ (asterisk sign) Indirection Access the value of an address.


operator

❖ Array Name as Pointers

An array name contains the address of the first element of the array
which acts like a constant pointer. It means, the address stored in the
array name can’t be changed.

©Topperworld
C++ Programming

For example, if we have an array named val then val and &val[0] can be used
interchangeably.

Example:

#include <iostream>

int main() {
int arr[] = {10, 20, 30, 40, 50};

int* ptr = arr; // Array name 'arr' is treated as a pointer to its first
element

std::cout << "First element: " << *ptr << std::endl;

ptr++; // Move to the second element


std::cout << "Second element: " << *ptr << std::endl;
return 0;
}

Output:

First element: 10
Second element: 20

©Topperworld
C++ Programming

❖ Pointer Expressions and Pointer Arithmetic


A limited set of arithmetic operations can be performed on pointers which
are:
➢ incremented ( ++ )
➢ decremented ( — )
➢ an integer may be added to a pointer ( + or += )
➢ an integer may be subtracted from a pointer ( – or -= )
➢ difference between two pointers (p1-p2)

❖ Advanced Pointer Notation


Consider pointer notation for the two-dimensional numeric arrays. consider
the following declaration:
int nums[2][3] = { { 16, 18, 20 }, { 25, 26, 27 } };
In general, nums[ i ][ j ] is equivalent to *(*(nums+i)+j)

❖ Pointers to pointers

In C++, we can create a pointer to a pointer that in turn may point to


data or another pointer.
The syntax simply requires the unary operator (*) for each level of indirection
while declaring the pointer.
char a;
char *b;

©Topperworld
C++ Programming

char ** c;
a = ’g’;
b = &a;
c = &b;
Here b points to a char that stores ‘g’ and c points to the pointer b.

❖ Void Pointers

➢ This is a special type of pointer available in C++ which represents the


absence of type.
➢ Void pointers are pointers that point to a value that has no type (and
thus also an undetermined length and undetermined dereferencing
properties). This means that void pointers have great flexibility as they
can point to any data type.
➢ There is a payoff for this flexibility.
➢ These pointers cannot be directly dereferenced. They have to be first
transformed into some other pointer type that points to a concrete data
type before being dereferenced.
Example:
#include <iostream>
int main() {
int num = 42;
float pi = 3.14159;

void* ptr; // Declare a void pointer

ptr = &num; // Assign the address of 'num' to the void pointer


std::cout << "Value pointed to by void pointer: " <<
*static_cast<int*>(ptr) << std::endl;
ptr = &pi; // Assign the address of 'pi' to the void pointer

©Topperworld
C++ Programming

std::cout << "Value pointed to by void pointer: " <<


*static_cast<float*>(ptr) << std::endl;

return 0;
}

Output:

Value pointed to by void pointer: 42


Value pointed to by void pointer: 3.14159

❖ Invalid pointers

A pointer should point to a valid address but not necessarily to valid elements
(like for arrays). These are called invalid pointers.
Uninitialized pointers are also invalid pointers.
int *ptr1;
int arr[10];
int *ptr2 = arr+20;
Here, ptr1 is uninitialized so it becomes an invalid pointer and ptr2 is out of
bounds of arr so it also becomes an invalid pointer.
Note: invalid pointers do not necessarily raise compile errors

©Topperworld
C++ Programming

❖ NULL Pointers

A null pointer is a pointer that point nowhere and not just an invalid address.
Following are 2 methods to assign a pointer as NULL;
int *ptr1 = 0;
int *ptr2 = NULL;

❖ Advantages of Pointers

⚫ Pointers reduce the code and improve performance. They are used to
retrieve strings, trees, arrays, structures, and functions.
⚫ Pointers allow us to return multiple values from functions.
⚫ In addition to this, pointers allow us to access a memory location in
the computer’s memory.

©Topperworld

You might also like