Constant Pointer in C
In C programming language, a pointer is a variable that stores the memory address of another variable. Pointers provide a way to indirectly access and manipulate the values of variables, by using their memory addresses.
Declaring a pointer in C involves two parts:
1. Declare a pointer variable: A pointer variable is a variable that stores the memory address of another variable. The pointer variable is declared with an asterisk (*) symbol before the variable name.
For example, to declare a pointer to an integer variable, you would write:
int *ptr;
2. Assign a value to the pointer variable: After declaring a pointer variable, you need to assign it the memory address of another variable. This is done using the address-of operator (&) before the variable name.
For example, to assign a pointer variable ptr to point to an integer variable x, you should write:
int x = 10;
int *ptr = &x;
In this example, the address of x is obtained using the address-of operator (&), and stored in the pointer variable ptr.
For example, to print the value of x using the pointer variable ptr, you would write:
printf("%d", *ptr);
In this example, the dereference operator (*) is used to get the value stored at the memory address stored in ptr, which is the value of x.
Pointers are powerful features of the C programming language, but they can also be complex and error-prone. It's important to be careful when working with pointers, to avoid errors such as accessing invalid memory addresses or dereferencing null pointers.
To declare a constant pointer in C, you need to use the const keyword before the pointer variable name. For example, the following declaration creates a constant pointer to an integer value:
const int* ptr;
This declaration indicates that the value pointed to by ptr is constant and cannot be changed. However, the pointer variable ptr itself is not constant and can be reassigned to point to a different integer constant.
Alternatively, you can also declare a constant pointer using the following syntax:
int* constptr;
This declaration creates a constant pointer to an integer value, which means that the pointer variable ptr itself is constant and cannot be reassigned to point to a different integer constant. However, the value pointed to by ptr can be modified.
It is important to note that a constant pointer and a pointer to a constant value are not the same thing. A constant pointer can be reassigned to point to a different constant value, whereas a pointer to a constant value cannot be used to modify the value it points to.
Here's an example of a constant pointer in C:
#include <stdio.h>
int main() {
const int num = 5; // Declare a constant integer variable
const int* ptr; // Declare a constant pointer to an integer value
ptr = # // Assign the address of num to the pointer
// Try to change the value pointed to by ptr
// This will result in a compiler error since the value is constant
*ptr == 10;
// Try to reassign the pointer to point to a different constant value
// This is allowed since the pointer itself is not constant
const int new_num = 8;
ptr = &new_num;
// Print the values of num and the new_num
printf("num = %d\n", num);
printf("new_num = %d\n", new_num);
// Print the values pointed to by the pointer
printf("*ptr = %d\n", *ptr);
return 0;
}
Output:
num = 5
new_num = 8
*ptr = 8
In this example, we first declare a constant integer variable num with a value of 5. Then we declare a constant pointer to an integer value ptr using the const int* syntax.
We assign the address of num to the pointer ptr. Since the value pointed to by ptr is constant, any attempt to modify it will result in a compiler error.
Next, we declare another constant integer variable new_num with a value of 8. We then reassign the pointer ptr to point to the address of new_num. Since the pointer itself is not constant, this is allowed.
Finally, we print the values of num and new_num and the value pointed to by the pointer ptr. Note that the value of num and new_num remain unchanged, but the value pointed to by the pointer ptr now points to new_num.