Difference Between Pointers and Array Notations in C++
Last Updated :
05 Jun, 2024
In C++, pointers and array notations are two ways using which we work with arrays and memory for accessing the data. They have distinct behaviours and are used in different contexts. In this article, we will learn the key differences between pointers and array notations in C++.
Difference Between Pointer Notation and Array Notations in C++
The below table demonstrates the key differences between pointers and array notations:
Feature | Pointer Notation | Array Notation |
---|
Declaration | dataType *pointerName; | arrName[index]; |
---|
sizeof Operator Behaviour | The sizeof operator will give the size of the pointer. | It will give the size of the whole array.
|
---|
Memory Allocation | In pointers allocation is done during runtime using new operator. | In arrays, allocation is done during compile time. |
---|
Nature
| The pointerName is mutable variable. We can increment and decrement the pointerName.
| arrName is not a mutable variable. We cannot increment arrName identifier as it is constant.
|
---|
Apart from the above differences, pointer notation and array notation can be used interchangeably in the program.
Pointer Notation in C++
In C++, a pointer is an object that is used to store the memory address of another object which allows the pointer to access the data stored in another object and manipulate it. Pointers are mainly used for dynamic memory allocation and deallocation.
- Now, if we allocate multiple element block using new to this pointer, we can use it arrays with the index pointing to the (n-1)th element in the block.
- We can also use the pointer as array if we assign the address of the first element of an array to the pointer.
Syntax of Pointer to Array in C++
dataType *ptrName = &anyArray[0];
or
dataType *ptrName = new int[size]
Here, the pointer is indicated by an asterisk(*) before the ptrName.
- dataType is the data type of the variable the pointer is pointing to.
- ptrName is the name of the pointer.
- anyArray is the name of some array.
- size is the size of the array.
Example
The below example demonstrates the use of pointer to access a value of a variable in C++.
C++
// C++ program to demonstrate the usage of pointer
#include <iostream>
using namespace std;
// utility function to print array
void printArr(int* arr, int size)
{
for (int i = 0; i < size; i++) {
// Access and print array elements using the pointer
cout << arr[i] << " ";
}
cout << endl;
}
// driver code
int main()
{
// size of the array
int size = 5;
// Define and initialize an array
int arr[] = { 10, 22, 30, 44, 50 };
// Pointer to the first element of the array
int* ptr = arr;
int* ptr2 = new int[size];
for (int i = 0; i < size; i++) {
ptr2[i] = i + 1;
}
cout << "Elements of Static Array: ";
printArr(ptr, size);
cout << "Elements of Dynamic Array: ";
printArr(ptr2, size);
return 0;
}
OutputElements of Static Array: 10 22 30 44 50
Elements of Dynamic Array: 1 2 3 4 5
To learn more about pointers in C++ refer: C++ Pointers
Array Notations in C++
In C++, an array is collection of elements that have the same type and are referred to by a common name. In arrays, elements are accessed using array notation. We can access and assign new value using array indexing. The name of an array acts as a constant pointer to the first element. For example, arr can be used as a pointer to arr[0].
Syntax of Array Notations
arrName[index];
Here,
- arrayName is the name of the array.
- index is the position in an array that you want to access or manipulate.
Example
The below example demonstrates the use of array notation in C++.
C++
// C++ program to use array notation
#include <iostream>
using namespace std;
int main()
{
// define an array
int arr[5] = { 10, 22, 30, 44, 50 };
// access 0th index element using array notation and
// print it
cout << "First element: " << arr[0] << endl;
return 0;
}
To learn more about arrays in C++ refer: C++ Arrays
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read