Constant Pointer, Pointer to constant and Constant pointer to constant

Constant pointer, Pointer to Constant and constant pointer to constant
Type of Pointer in C language

A pointer that constantly points to the same address of its type throughout the program is known as a constant pointer whereas A pointer that points to a constant[ value at that address can’t be changed by this pointer] is termed as a pointer to constant.

Advertisements

Let’s understand it by an example, but before that check out the previous post :-

Constant Pointer

As named constant pointer constantly points to a fixed memory address of its type. It can’t be re-assigned or modified to another address. In case the developer wants to do so the compiler will raise an illegal operation error but the value at that pointed address can modify.

//Syntex
<type of pointer> *const <name of pointer>;
  
#include <stdio.h>
    int main(void) { 
    //Decleration 
    int a = 10;          //Variable 
    int * const b = &a;  // Constant pointer 

    //Re-assigning 
    int c=15;       //Another variable
    b = & c;   // Error , because constant pointer can't be modify 
	return 0;
}

prog.c: In function ‘main’:
prog.c:11:3: error: assignment of read-only variable ‘b’
b = & c;

Advertisements

Pointer to constant

A pointer that constantly points to a fixed value at the memory address. we can’t modify that value using this pointer but we definitely can modify or re-assign the pointer to another address.

//Syntex
const <type of pointer>  * <name of pointer>;
or
<type of pointer>  const * <name of pointer>;

#include <stdio.h>
int main(void) {
      //Decleration
      int a = 10;          //Variable
      const int * b = &a; // Pointer to constant      

//Re-assigning
      int c=15;     //Another variable
     b = & c;      //Re-assigned or modify
    *b = 25; // Error Value modification not allowed using pointer to constant 
  return 0;
 }

Compilation error #stdincompilation error#stdout 0s 5396KB
prog.c: In function ‘main’:
prog.c:10:6: error: assignment of read-only location ‘*b’
   *b = 25;
Advertisements

Constant Pointer to constant

A pointer that neither can be modified or re-assigned nor the pointed value can be modified is termed as a constant pointer to constant.

//Syntex
const <type of pointer>  *  const <name of pointer>;

#include <stdio.h>
int main(void) {
      //Decleration
      int a = 10;       //Variable
      const int * const b = &a; // constant Pointer to constant      

//Re-assigning
      int c=15;   //Another variable
     b = & c;    //Error,  Re-assigned or modify not allowed
    *b = 25;     // Error,  Value modification not allowed
  return 0;
 }
prog.c: In function ‘main’:
prog.c:10:8: error: assignment of read-only variable ‘b’
      b = & c;                          
        ^
prog.c:11:8: error: assignment of read-only location ‘*b’
     *b = 25;
Advertisements

Applications of Different kind of pointers

When you’re designing C programs for embedded systems, or special purpose programs that need to refer to the same memory (multi-processor applications sharing memory) then you need constant pointers.

For instance, I have a 32-bit MIPS processor that has a little LCD attached to it. I have to write my LCD data to a specific port in memory, which then gets sent to the LCD controller.

Pointer to constant is commonly used while passing a pointer as a parameter where value modification is not allowed or value is read-only.
Constant pointer to constant, For example, knowing the status of specific interrupt through the pointer.

Advertisements

Leave a comment

Leave a comment