Showing posts with label const example. Show all posts
Showing posts with label const example. Show all posts

Thursday, August 28, 2014

constant pointer and pointer to constant and both


Syntax:
  • constant pointer
    • <type of pointer> * const <name of pointer>        ( int * const ptr;)
    • once address assigned then that's it. address  is constatnt. but that address can have any value
    • thinking like used to store particular hardware register using this kind of pointer and never changed that pointer address/restrict to change that pointer address throughout the program
  • pointer to constant
    • const <type of pointer>* <name of pointer>         (const int* ptr;)
    • once address assigned then address's value is constant but can change address
    • thinking like used in the receving function parameter and not allowing the function to change the value (void func(const int* ptr))
  • constant pointer to constant
    • const <type of pointer>* const <name of pointer>(const int* const ptr;)
    • both the assigned address cannot be changed and address's value also cannot be changed
    • combination of above both
-------------------------------------------------------------------------------------------------------------------------------
Purpose of const qualifer :

  • ows the compiler to catch errors in which code accidentally changes the value of a variable
  • he compiler might be able to make certain optimizations to the code generated if it knows that a variable will not be changed.
  • example 1:
  • while (*str = 0) /*instead of *str != 0  mentioned as *str = 0 by progmer*/
    {
        /* some code here */
        str++;
    }

  • constant pointer
    • <type of pointer> * const <name of pointer>        ( int * const ptr;)
    • once address assigned then that's it. address value is constatnt

Example 1a:

root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# cat constant_pointer.c 

#include<stdio.h>
int main()
{
int a=10;
int b=20;
int * const ptr=&a;
ptr=&b;
return 0;
}

root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# gcc constant_pointer.c 
constant_pointer.c: In function ‘main’:
constant_pointer.c:8:2: error: assignment of read-only variable ‘ptr’
root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# 

Example 1b:

root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# cat constant_pointer.c 
#include<stdio.h>
/* */
int main()
{
int a=10;
int b=20;
int * const ptr=&a;
printf("\nvalue before changing  value of a = %d\n",*ptr);
*ptr = 20;
printf("\nvalue after changing of value of a = %d\n",*ptr);
//ptr=&b;
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# gcc constant_pointer.c 
root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# ./a.out 

value before changing  value of a = 10

value after changing of value of a = 20
root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# 

-------------------------------------------------------------------------------------------------------------------------------
  • pointer to constant
    • const <type of pointer>* <name of pointer>         (const int* ptr;)
    • once address assigned then address's value is constant but can change address

Example 2a:
root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# cat pointer_to_constant.c 

#include<stdio.h>
int main()
{
int a=10;
int b=20;
const int *ptr=&a;
*ptr = 20;
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# gcc pointer_to_constant.c 
pointer_to_constant.c: In function ‘main’:
pointer_to_constant.c:8:2: error: assignment of read-only location ‘*ptr’
root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# 

Example 2b:

root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# cat pointer_to_constant_2.c 
#include<stdio.h>
/* */
int main()
{
int a=10;
int b=20;
const int *ptr=&a;
ptr = &b;
printf("\n%d\n",*ptr);
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# gcc pointer_to_constant_2.c 
root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# ./a.out 

20
root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# 

---------------------------------------------------------------------------------------------------------------------------------
  • constant pointer to constant
    • const <type of pointer>* const <name of pointer>(const int* const ptr;)
    • both the assigned address cannot be changed and address's value also cannot be changed
root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# cat constant_pointer_to_constant.c 
#include<stdio.h>
int main()
{
int a=10;
int b=20;
const int * const ptr=&a;
*ptr = 30;
ptr = &b;
printf("\n%d\n",*ptr);
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# gcc constant_pointer_to_constant.c 
constant_pointer_to_constant.c: In function ‘main’:
constant_pointer_to_constant.c:7:2: error: assignment of read-only location ‘*ptr’
constant_pointer_to_constant.c:8:2: error: assignment of read-only variable ‘ptr’
root@selvakumar-Lenovo-B460e:/C_pgms/const_pointer# 

Reference:

http://www.thegeekstuff.com/2012/06/c-constant-pointers/
https://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const