Const pointer in C

The constant keyword is used as “const,” and it specifies that the pointer cannot be modified after initialization; that is, the pointer is protected from modification. The const keyword can be applied to the declaration of any variable to specify that its value will not change depending on where the const variables are stored. We can change the value of a constant variable using a pointer. The “const” pointer of a particular type can be assigned to a pointer of a similar type. Although, a pointer that is not a constant can never be set to a “const” pointer.

A constant pointer using a “const” keyword is a pointer that cannot change the address it will be holding. In other terms, once a constant pointer points to the variable, then it cannot be used to point to any variable other than the specified one. One should keep in mind that the const pointer’s value can be changed, but the const pointer’s address cannot be altered at any point in time.

Syntax:

  • The data type of the pointer: This is all about the data type of the variable which the program or the compiler holds. It can be char, int, float according to the application’s requirement.
  • Const: This attribute is used to inform the C compiler about the variable behavior which is used in the program. It lets the compiler know whatever the particular variable’s address might be, and it will remain the same throughout the rest of the program. As the word constant defines “situation that does not change,” the memory address cannot be changed in the same way. If in any way changed, the compiler will throw an error.
  • Name of the pointer: This is the name of the pointer that will hold the address of the variable. When we pass a print statement, we can see the output of holding the variable address.
  • *: An asterisk (*), as defined, will represent the value of the variable held by the pointer.

E.g.:

Defining the const means we are informing the compiler about the pointer “ptr” variable. The compiler infers that this is a variable that will hold the address of another variable integer. If the value pointed by the pointer changes, the compiler will throw an error. To change the pointer, we have to assign it using a different pointer variable. First, we need to declare the data type of the pointer to define and declare a pointer. This informs the C compiler about the data type of the variable the pointer will hold. The following important attribute a programmer needs to take care of is the keyword const. It informs the C compiler that any variable address it will hold and tells it will be constant for the rest of the program. The upcoming important attribute of the constant pointer is the name of the pointer. It will be the address of the variable. * as said earlier, represents the pointer variable

It can be inferred that the declaration of two variables, a and b, are declared along with the constant pointer “ptr”. This constant pointer “ptr” is made to point the variable; but later, pointer “ptr” is made to point variable b. Ultimately, we try to print the value “ptr” pointing to. We have assigned an address to a constant pointer, and then we are trying to change the address by assigning the address of some other variable to the same constant pointer. This will produce an error as the pointer again points to a variable.

Constant pointer to a constant

A constant pointer to a constant is the pointer that can neither change the address it is pointing to nor it can change the value kept at that address.

Syntax:

E.g.:

In this case, we have declared two variables a and b along with the pointer to a constant. This pointer to a constant “ptr” is made to point the variable b. Later this is again made to change.

Pin It on Pinterest

Share This