简体   繁体   中英

Difference between normal pointer and const pointer in c

I don't know the difference between a normal pointer and a const pointer. The below code works fine, but when I change int *ptr=# to int *const ptr = &var1; then it does not. What is the difference between a normal pointer and a const pointer?

 int main(void)
    {
        int num = 20;
     int  *ptr = &num ; // if i change to   `int *const ptr = &var1;` then it shows some error

     *ptr = 20 ;              // Valid 
     ptr ++ ;                 // valid

        return 0;
    }
int* const ptr = &num ;

Will create a constant pointer to an int. The data it points to can be changed, but the pointer it self cannot.

You cannot change the pointer:

ptr++ ;

But you can change the data:

*ptr = 1234 ;

We can do following operations on constant pointers

  • Assigning value at address
  • Printing value or address
  • Assigning Address at the time of declaration.

We Can't to following operation on constant pointers

  • Adding Integers to Constant pointers.
  • Subtracting integers to constant pointers.
  • Any Operation that can change address of pointer.

So, Here in your question..

If you declare

int* const ptr = &num ; // this is ok

next line

*ptr = 20 ;  // Assigning value at address this is ok

Now,

ptr ++ ;  // you can not change the value // Error!

Hope it helps!

This:

int* const ptr = &num ;

will create a constant pointer to an integer. You can use it to modify the integer's value but you can't change where the pointer points, thus ptr++ ; is invalid.

The const keyword is usually applied to its left symbol, eg

int * const ptr; // A constant pointer (*)
int const * ptr; // A pointer to a constant integer
int const * const ptr; // A constant pointer to a constant integer
const int *ptr; // Shorthand for pointer to a constant integer (equivalent to int const * ptr;)

const pointers are useful when you want to pass a fixed memory location around and you want to make sure that nobody will modify the pointer's pointed address.

in c, const is a type qualifier . use of const in some variable definition means, the variable will not get modified (will be treated as read-only )during the entire lifetime of the program.

Usually, when defining a variable / data type with const , the pratice is to initialize it with required value, as normally, the value it holds cannot be modified at a later part.

For example:

const int a = 10;

means, the integer a will hold the value 10 and it cannot be changed. at a later part,

a = 20;

will produce error.

So, in your case

int *const ptr = &var;

here, ptr will always hold the address of var and it cannot be changed, ie, we cannot write

ptr = &num2; // where num2 is another int, declared like int num2;

it will show compile-time error like :

error:assignment of read-only variable "*ptr".

You can find a nice and handy description here .

int* const pointer = &x ;

it create a constant pointer to an int. The data it points to can be changed, but the pointer it self cannot be changed

You cannot change the pointer:

pointer++ ;

here you can change the data:

*pointer=1 ;


In case of normal pointer , both the value of pointer & value @ pointer can be change.But as the pointer change the value @ pointer automatically change to some garbage value.

#include <stdio.h>

int main () {

   int val = 5;
   int *ptr = (int*)&val;

   printf("val@ptr : %d \nptr     : %x\n", *(ptr), (int*)ptr);

   val++;
   printf("\nIncrement val++\n");
   printf("val@ptr : %d \nptr     : %x\n", *(ptr), (int*)ptr);

   ptr++;
   printf("\nIncrement ptr++\n");
   printf("val@ptr : %d \nptr     : %x\n", *(ptr), (int*)ptr);

   return 0;
}

Output:

val@ptr : 5 
ptr     : 93ddc274

Increment val++
val@ptr : 6 
ptr     : 93ddc274

Increment ptr++
val@ptr : -1814183304 
ptr     : 93ddc278


But in case of const pointer , only the value @ pointer can be change not the pointer. See the below example.

#include <stdio.h>

int main () {

   int val = 10;
   //always start reading from right to left
   int *const ptr = (int*)&val;//ptr is const pointer to int, i.e. ptr can not be change at all.

   printf("The value1 @ptr : %d\t and ptr val : %x\n", *(ptr), (int*)ptr);
   val++;
   //ptr++;
   printf("The value1 @ptr : %d\t and ptr val : %x\n", *(ptr), (int*)ptr);

   return 0;
}

Output:

The value1 @ptr : 10     and ptr val : ee2ccf24
The value1 @ptr : 11     and ptr val : ee2ccf24

If we un-comment the line number 11, then output would be like this:

main.c:11:7: error: increment of read-only variable ‘ptr’
    ptr++;

Please go through the link to get better understanding : http://c-faq.com/decl/spiral.anderson.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM