Write about Constant Pointers in C


Constant Pointers
A constant pointer is a pointer that cannot change the address its holding. In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable.
A constant pointer is declared as follows :
< type of pointer >  *  const < name of pointer >
An example declaration would look like :
int * const ptr;
Lets take a small code to illustrate these type of pointers :
#include

int main(void)
{
    int var1 = 0, var2 = 0;
    int *const ptr = &var1;
    ptr = &var2;
    printf("%d\n", *ptr);

    return 0;
}
In the above example :
  • We declared two variables var1 and var2
  • A constant pointer ‘ptr’ was declared and made to point var1
  • Next, ptr is made to point var2.
  • Finally, we try to print the value ptr is pointing to.
So, in a nutshell, we assigned an address to a constant pointer and then tried to change the address by assigning the address of some other variable to the same constant pointer.

Related

What is a Variable? How to Declare and Assign values to Variables?

Variable: Variable is a named memory location that can store some value unlike constants a Variable may take different values at different times during execution. A ...

Explain Different Data Types Available in ‘C’?

C language supports a rich set of data types the variety of data types available allow the programmer to select the type appropriate to the needs of the application. ANSI C support...

Write about Symbolic Constants (or) #define statement

The constants which will be used in number of places in a program can be defined using # define statement. Ex:    #define PI 3.14          #define...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item