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
Difference between multidimensional array in C++ and Java Prerequisites: Multidimensional array in C++, Multidimensional array in Java Multidimensional Arrays: Multidimensional arrays are a tabular representation of arrays to store multiple elements. These dimensions can be 1D arrays, 2D-arrays, etc. Multidimensional arrays are available in both C++ and Ja
4 min read
Difference Between string and char[] Types in C++ In C++, we can store the sequence of characters i.e. string in two ways: either as a std::string object or char array. In this article, we will discuss some major differences between string and char[] in C++.Character Array Type StringsA character array is simply an array in C++ that contains charac
2 min read
Difference Between Array of Characters and std::string in C++ In C++, we have character array and std::string class both of which are used to store the sequence of characters. The character arrays are a part of C-style programming on the other hand std::string is a part of the C++ standard library. In this article, we will discuss what are some major differenc
3 min read
Difference Between *& and **& in C++ In C++, the *& (pointer to reference) and **&(pointer to pointer reference) symbols are used in the context of pointers and references for manipulating memory addresses and dealing with complex data structures. While they seem similar, they serve different purposes. In this article, we will
5 min read
Difference between int (*p)[3] and int* p[3]? Pointers store the address of variables or a memory location. Pointers are a symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Its general declaration in C/C++ has the format: Syntax: datatype *var_na
2 min read
Difference Between Struct and Typedef Struct in C++ In C++, the struct keyword is used to define a struct, whereas the typedef keyword is used for creating an alias(new name) for existing datatypes and user-defined datatypes like class, struct, and union to give them more meaningful names. In this article, we will learn the differences between a stru
3 min read
Difference between std::set vs std::vector in C++ STL Vectors: Vectors are containers similar to dynamic arrays, with the ability to resize when a new element is inserted or deleted from it. It is a template of Standard Template Library or STL, which provides more flexibility to the program. Elements of vectors are placed in contiguous storage and are
2 min read
Difference between passing pointer to pointer and address of pointer to any function In this article, the differences between passing âpointer to pointerâ and âaddress of pointerâ to a function. In C or C++ Programming Language, it is known that pointers hold the address of the variables or any memory location. If pointers are pointed to the memory location, it can be used to change
3 min read
Creating array of pointers in C++ An array of pointers is an array of pointer variables. It is also known as pointer arrays. We will discuss how to create a 1D and 2D array of pointers dynamically. The word dynamic signifies that the memory is allocated during the runtime, and it allocates memory in Heap Section. In a Stack, memory
5 min read
Difference Between Stack-Allocated and Heap-Allocated Arrays In C/C++, arrays can be allocated in two areas of memory: the stack and the heap. Each has its own characteristics and use cases. In this article, we will see the key differences between stack-allocated and heap-allocated arrays. Stack-Allocated ArraysThe arrays declared as static arrays in the func
3 min read