This article will explain that why and how pointers and arrays are similar. There is a quick introduction about pointer constants and pointer variables because these terms are used in this article very often. The place where the address of a variable can be stored is called pointer variable and the address of that variable which you want to store is called pointer constant.
int i; variable integer i is declared.
int *p; variable p pointer to integer is declared
P = &i; p is initialized with address of i
Suppose integer variable i is initialized at memory location 401. In this case ‘p’ is a pointer variable and the value which is stored in pointer variable (401) is a pointer constant.
Now start with the current topic that how pointers and arrays work similarly. Initialize an integer array of size 3.
int a[3];
Suppose that array ‘a’ is initialized at memory location 301. When you write a[3] in your program in fact you ask the compiler to allocate the memory size of 4*3 bytes (integer size is 4 bytes) and compiler allocates 12 bytes in memory. According to the c/c++ specifications, the name of the array is the starting address of that array. It means that in your program where ever the compiler will find ‘a’, it will consider it as the address of that array (in this case 301). In other words you can say that ‘a’ is representing the memory location and ‘a’ is a pointer constant.
If it is true that ‘a’ is a pointer constant then it means that you can perform all operations as you can perform with normal pointers. Recall some pointer functionalities.
1 Data can be accessed by pointer.
2 Pointer arithmetic can be performed.
3 Value of one pointer can be assigned to another pointer.
Now assign some values to array
a[0] = 10;
a[1]= 20;
a[2]= 30;
You can access data using pointer ‘a’
printf(“value of a[0] %d \n”, *a);
in this line of code you tell the compiler that go to that address where the pointer ‘a’ is pointing at and display the value on the screen.
You can perform pointer arithmetic.
printf(“value of a[1] %d \n”, *(a+1));
in this line you tell the compiler that go to that address, apply pointer arithmetic, skip some bytes from the current location and display the value on the screen. You know that according to the rules of pointer arithmetic the pointer will skip the bytes, according to the data type it points to. In this case pointer will skip 4 bytes because size of integer is 4 bytes.
printf(“value of a[0] %d \n”, *(a+2));
in this line of code pointer will skip 8 bytes.
you can also assign the address of one pointer to another pointer. Here is a pointer variable p.
int *p;
p = a;
p is initialized with ‘a’. You can notice that address of (&) operator is not used to assign the address of ‘a’ to ‘p’ because ‘a’ itself representing the address of memory location.
printf(“value of a[0] by using pointer p %d \n”,*p);
in this line of code you tell the compiler that read the variable p, fetch the address, go to that location and print the value on screen.
printf(“value of a[1] by using pointer p %d \n”,*(p+1));
printf(“value of a[2] by using pointer p %d \n”,*(p+2));
in this line of code you tell the compiler that read the variable p, fetch the address, go to that location, perform pointer arithmetic and print the value on screen.
Here is an interesting point related to the subscript operator ‘[]’. This operator is used for declaration of array. According to the c/c++ specifications all arrays are zero based. It means if you want to access the first element of the array, you will write a[0], if you want to access second element of array you will write a[1] and if you want to access nth element of the array you will write a[n-1]. At the beginning it is discussed that name of the array is a pointer constant and we can access the value of that pointer by using ‘*a’. if you look closely then you will realize that a[0] and *(a) are the same and a[1] and *(a+1) are the same. By using subscript operator [] you can access the element of that array. But do you know that you can use the subscript operator with pointer variable also. Yes you can, because p[1] and *(p+1) are same. You can use the subscript operator with pointer constant and pointer variable.
Int *p;
p = a;
printf(“value of a[0] by using pointer p %d \n”,p[0]);
At the end of this article there is very short question for you. You have assigned the value of ‘a’ to variable pointer p
p = a;
but can you do a = p ?
why or why no. tell me about this.
#include <iostream>
using namespace std;
int main()
{
int a[3];
a[0] = 10;
a[1] = 20;
a[2] = 30;
int *p;
printf("a[0] is %d\n", a[0]);
printf("a[1] is %d\n", a[1]);
printf("a[2] is %d\n", a[2]);
// value of array by using pointer and pointer arithmetic
printf("a[0] is %d\n", *(a));
printf("a[1] is %d\n", *(a+1));
printf("a[2] is %d\n", *(a+2));
p = a;
// value of array by using pointer variable and pointer arithmetic
printf("value of a[0] by using pointer p %d \n",*(p));
printf("value of a[1] by using pointer p %d \n",*(p+1));
printf("value of a[2] by using pointer p %d \n",*(p+2));
printf("value of a[0] by using subscript operator %d \n",p[0]);
printf("value of a[1] by using subscript operator %d \n",p[1]);
printf("value of a[2] by using subscript operator %d \n",p[2]);
return 0;
}
related topics:
Pointer arithmetic
Pointers. Pointer to constant and constant pointer
Pointers. An indirect data access
Pointer Type Casting
If you have some question/comment, you are welcome to ask.










August 16, 2011 at 3:57 pm
You didn’t point the difference between pointer to a block of memory and arrays.
The access mechanism of a[i] and b[i] are different.
August 17, 2011 at 5:42 pm
Hi phoxis… you are right actually there are few differences also in pointers and arrays but i wanted to keep focus on one dimenshion but i have this point in my mind when i will come to the dynamic memory allocation the i will point it out. Thanks for your concern and comments
August 27, 2011 at 8:12 pm
thnxxxxxxxxxxxxxxxxxxxxxxxxxx………