C++ const pointer
When we declare and initialize a variable x with 10, memory is reserved for x with name x. if we display x as
cout<<x;
The value 10 stored at memory location named x will be displayed
Similarly in case of arrays, suppose an array as int y[10]. This means that we have reserved memory locations for ten integers and named it collectively as y. Now we will see what actually y is? For this we will display y as
cout<<y;
Unlike variable x, array name y display memory address at output. 'y' represents the memory address of the beginning of this collective memory locations. The first element of the array can be accessed as y[0]. Remember arrays index starts from 0 in C language, so the memory address of first element i.e. y[0] is stored in y. It concludes that
“The name of the array is a constant pointer which contains the memory address of the first element of the array”
Variable Pointer and Constant Pointer
In case of variable pointer, we can assign new memory address at any time as
int x = 10;
int *ptr = &x;
int y = 12;
ptr = &y;
int z[3]= {5,4,6};
ptr = z;
In above code
- We declare and initialize a variable x with value 10
- Assign memory address of x to pointer ptr
- Declare another variable y
- Assign memory address of y to pointer ptr
- Declare int array name z
- Assign array z to ptr
It concludes that ptr is variable pointer and can re-initialize with new address at any time.
Last two lines of code are important. The value of z i.e. the address of the first element of the array is assigned to ptr. Now we have two things pointing to the same place, z and ptr. Both are pointing to the first element of the array. However, z is a constant pointer and always points to the same location whereas ptr is a pointer variable that can also point to any other memory address.
In case of constant pointer as array name, we cannot assign another memory address because array name always points to the first element of array or we can say that array name always contains the memory address of first element of array as
int a[5]={4,5,6,2,9};
cout<<a;
int x = 10;
a = &x; //Error
In last line, we are trying to assign new memory address to array name (const pointer in c). As it is not possible so compiler generates error.
There are three ways to access array elements
Below we are declaring an array c of size 4 and assigning its memory address to a pointer cptr
int c[4]={9,4,8,6};
int *cptr = c;
Now we will access array elements by three different ways
Access array elements using array name (Constant Pointer)
cout<<"Mem add of first element:";
cout<<c;
//using Reference operator
cout<<"Value of first element:";
cout<<*c;
cout<<"Mem add of 2nd element:";
v<<c+1;
//using Reference operator
cout<<"Value of 2nd element:";
v<<*(c+1);
Access array elements using index operator
cout<<"Mem add of first element:";
cout<<&c[0];
cout<<"Value of first element:";
cout<<c[0];
cout<<"Mem add of 2nd element:";
cout<<&c[1];
cout<<"Value of 2nd element:";
cout<<c[1];
Access array elements using variable pointer
cout<<"Mem add of first element:";
cout<<cptr;
cout<<"Value of first element :";
cout<<*cptr;
cout<<"Mem add of 2nd element:";
cout<<cptr+1;
cout<<"value of 2nd element:";
cout<<*(cptr+1);
We can store memory address of any element in pointer as below we are assigning address of 3rd element to pointer cptr
cptr = &c[2];
Below is a C++ program about pointer and array. This program demonstrate how to access array elements using constant pointer, variable pointer and index operators.
Pointer and Array |
Output
Pointer and array output |
Post a Comment