in Technology by
Differentiate between a constant pointer and pointer to a constant?

1 Answer

0 votes
by

Constant pointer:

A constant pointer is a pointer whose value (pointed address) is not modifiable. If you will try to modify the pointer value, you will get the compiler error.

A constant pointer is declared as follows :

Data_Type * const Pointer_Name;
eg,
int *const ptr; //constant pointer to integer

 

Let’s see the below example code when you will compile the below code to get the compiler error.

#include<stdio.h>
int main(void)
{
int var1 = 10, var2 = 20;
//Initialize the pointer
int *const ptr = &var1;
//Try to modify the pointer value
ptr = &var2;
printf("%d\n", *ptr);
return 0;
}

 

Pointer to a constant:

In this scenario the value of the pointed address is constant that means we can not change the value of the address that is pointed by the pointer.

A constant pointer is declared as follows :

Data_Type const* Pointer_Name;
eg,
int const *ptr// pointer to const integer

 

Let’s take a small code to illustrate a pointer to a constant:

#include<stdio.h>
int main(void)
{
int var1 = 100;
// pointer to constant integer
const int* ptr = &var1;
//try to modify the value of pointed address
*ptr = 10;
printf("%d\n", *ptr);
return 0;
}

Related questions

0 votes
    What is a constant pointer in C?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    What is the difference between pointer to an array and array of pointers?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is the difference between an uninitialized pointer and a null pointer?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    To differentiate between singular and plural, __________. A. (?:text|texts) B. (.*text|texts) C. (*text|texts) D. (?text|texts)...
asked Dec 13, 2022 in Technology by JackTerrance
0 votes
    Is Scrum and Agile the same? If not so, differentiate between them....
asked Aug 19, 2021 in Technology by JackTerrance
0 votes
    How to differentiate between ActionResult and ViewResult?...
asked Jun 14, 2021 in Technology by JackTerrance
0 votes
    Differentiate between univariate, bivariate, and multivariate analysis....
asked Apr 26, 2021 in Technology by JackTerrance
0 votes
    Differentiate between Reusable Transformation and Mapplet....
asked Mar 29, 2021 in Technology by JackTerrance
0 votes
    Differentiate between joiner and Lookup Transformation....
asked Mar 27, 2021 in Technology by JackTerrance
0 votes
    Differentiate between Source Qualifier and Filter Transformation?...
asked Mar 27, 2021 in Technology by JackTerrance
0 votes
    Differentiate between TOS for Data Integration and TOS for Big Data?...
asked Mar 23, 2021 in Technology by JackTerrance
0 votes
    Differentiate between TOS for Data Integration and TOS for Big Data....
asked Mar 23, 2021 in Technology by JackTerrance
0 votes
    Differentiate between the usage of tJava, tJavaRow, and tJavaFlex components....
asked Mar 18, 2021 in Technology by JackTerrance
0 votes
    Differentiate between “insert or update” and “update or insert”....
asked Mar 18, 2021 in Technology by JackTerrance
0 votes
    Differentiate between ETL and ELT.in Talend?...
asked Mar 18, 2021 in Technology by JackTerrance
...