array and pointer in c++ with examples

Image result for c++Array and pointer

Going forward with this C/C++ programming language tutorial we will understand how to access array as a pointer.
We define array as shown below.
int A[10];
A[0] = 0; A[1] = 10; A[2] = 20; ...
Here A is pointer as well. A contains address of 1st int, hence A+1 contains address of 2nd int, A+2 contains address of 3rd int.
Since A is address hence *A will give 0(A[0] = 0 as defined above), value of *(A+1) will be 10, *(A+2) will be 20. Note that *(A)+1 and *(A+1) is not same. *(A)+1 means add 1 to value stored to address stored in A, but *(A+1) means value stored at the address next to A. So now to access value at any index(n) at array we have 2 ways. A[n] और *(A+n)
Lets see an example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
int main() {
  int A[] = {1,2,3,4};
  printf("[%d, %d, %d, %d]\n",A[0], A[1], A[2], A[3]);
  *A = 10;
  *(A+1) = 20;
  *(A+2) = 30;
  *(A+3) = 40;
  printf("[%d, %d, %d, %d]\n",A[0], A[1], A[2], A[3]);
  A[0] = 0; A[1] = 2; A[2] = 4; A[3] = 6;
  printf("[%d, %d, %d, %d]\n", *A, *(A+1), *(A+2), *(A+3));
  scanf("%d", A);
  return 0;
}
Run the program to see the output.