Pointers and Constants
Tags: beginners, cpp, pointer
Published: 2023-04-21 17:03:10

Pointer stores address of variable and it can be referenced and dereferenced. Pointers are powerful which enable developers to play at memory level operations. Sometimes developers may make accidental modifications to values pointed by pointers. To impose restriction on pointers **const** keyword can be used. Based on usage of _const_, there are 3 possible scenarios as below

- **Pointer to Constant** : Value pointed by pointer to constant can not be changed but pointer can changed. This type of pointers are used to restrict accidental modifications to value. It provide read only access to value to functions when passed as argument.

Pointer to constant can be declared by placing const keyword before data type.
Example. **const** int *p = NULL;

Below code snippet demonstrates restriction on value change.

```
#include<iostream>
using namespace std;
int main()
{
	const int k=20;
	const int *p = &k;
	cout<<*p;
	p = NULL; // can be changed
	p = p+1; // can be changed
	//*p =100; // Error assignment of read-only location '* p'
     //This error comes as pointer is declared as pointer to const
	return 0;
}
```

---

- **Constant pointer to variable** : Value pointed by constant pointer can be changed but pointer can not be changed. This type of pointer can hold same address for life time and it can not be changed to point to other variable. Constant pointers needs to be initialized where it is declared.

Constant Pointer can be declared by placing const keyword after * and before pointer name.
Example.
int a =10 ;
int * **const** p = &a;

Below code snippet demonstrates restriction on changing pointer. 

```
#include<iostream>
using namespace std;
int main()
{
	int k=20;
	int * const p = &k;
	cout<<*p<<endl;
	//p = NULL;// can  not be changed
	//p = p+1; // can  not be changed
	*p =100; // value ( *p) can be changed	 
	cout<<*p<<endl;
	return 0;
} 
```

Array name is internally a constant pointer to first element of array and it stores base address of array. Function name is also internally constant pointer.

Below snippet demonstrates array and function names restriction.

```
#include<iostream>
using namespace std;
int add(int a, int b)
{  
	return a+b; 
}

int main()
{
	char name[50]= "Priyanka";
	//name++; //error: lvalue required as increment operand
	//add = add+1; //error: assignment of function 'int add(int, int)'
	cout<<name;
	return 0;
}

```

---



- **Constant pointer to constant** : Value pointed by constant pointer to constant can not be changed also pointer can not be  changed. Here pointer and value both are read only. Its used to pass or insert **this** pointer in member functions of class.

Constant Pointer to constant can be declared by placing const keyword before datatype as well as  const after * and  before pointer name.
Example.
const int a =10 ;
**const** int * **const** p = &a

```
#include<iostream>
using namespace std;
int main()
{
	const int k=20;
	const int *const p = &k;
	cout<<*p<<endl;
	p = NULL; // Can not be changed error: assignment of read-only variable 'p'
	p = p+1; // Can not be changed error: assignment of read-only variable 'p'
	*p =100; // Can not be changed error: assignment of read-only location '*(const int*)p'
	cout<<*p<<endl;
	return 0;
} 


```
**Conclusion** :  Pointers are flexible and they support fastest data operations by directly working on addresses. While working with pointer if developers make use of const keyword they control write access to data.