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. For example, if we have an array named val then val and &val[0] can be used interchangeably.
// C++ program to illustrate Array Name as Pointers
#include <bits/stdc++.h>
using namespace std;
void geeks()
{
// Declare an array
int val[3] = { 5, 10, 20 };
// declare pointer variable
int* ptr;
// Assign the address of val[0] to ptr
// We can use ptr=&val[0];(both are same)
ptr = val;
cout << "Elements of the array are: ";
cout << ptr[0] << " " << ptr[1] << " " << ptr[2];
}
// Driver program
int main() { geeks(); }
Output
Elements of the array are: 5 10 20
If pointer ptr is sent to a function as an argument, the array val can be accessed in a similar fashion. Pointer vs Array
C++ Pointers
Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of pointers.
The address of the variable you’re working with is assigned to the pointer variable that points to the same data type (such as an int or string).
Syntax:
datatype *var_name;
int *ptr; // ptr can point to an address which holds int data