top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Pointer and Constant correlation in C?

+1 vote
398 views

There are three ways in which a constant can be correlated with pointer -
1. constant pointer
2. pointer to a constant
3. constant pointer to a constant

1. 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.

Sample Declaration: int * const ptr;

Sample code

int main()
{
  int num[2] = {1,2};
  int* const ptr = &num[0];

  printf("%d",*ptr); // This will print 1

  *ptr = 5;
  printf("%d",*ptr); // This will print 5

  ptr++; //This is an compilation error 
}

Note: We must initialize the pointer at the time of declaration as following will give the error.

int* const ptr;
ptr = &num[0];

2. Pointer to a Constant
A pointer through which cannot change the value of variable it points is known as a pointer to constant. These type of pointers can change the address they point to but cannot change the value at that address.

Sample Declaration: const int* ptr;

Sample Code

int main(void)
{
    int var1 = 0;
    const int* ptr = &var1;
    *ptr = 1; // Invalid operation as ptr is pointer to a constant
}

3. Constant Pointer to a Constant
This type is a pointer which is mix of first two which means that a pointer can not point to another variable neither change the value of the variable it point to.

Sample Declaration: const int* const ptr;

Sample Code

int main()
{
  int num[2] = {1,2};
  const int* const ptr = &num[0];

  printf("%d",*ptr); // This will print 1

  *ptr = 5; // This is a compilation error

  ptr++; //This is an compilation error 
} 
posted Jun 3, 2014 by Salil Agrawal

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

In this article I will discuss about the various arithmetic operations in C. Lets discuss them under the following three sub topics -

1. Increment/Decrement And Addition/Subtraction
2. Difference & Comparison
3. Invalid operations

1. Increment/Decrement And Addition/Subtraction

Increment or decrement of a pointer is dependent on the size of the variable on which we are applying the increment/decrement operation.

new value of the pointer =  current value (address) +  i * size_of(data type) 
new value of the pointer =  current value (address) -  i * size_of(data type)  

Behavior with the array
When pointer is pointing to the array and if we increase the pointer then the new value (address) contains would be

New Address of Pointer = Address of Pointer + i*(Size of Data Type)*(Size of Array)

Check the following example

#include<stdio.h>

int main(){

 float var[5]={1.01, 2.02, 3.03, 4.04, 5.05};
 float(*ptr)[5];

 ptr=&var;
 printf("Value inside ptr : %u",ptr);

 ptr=ptr+1;
 printf("Value inside ptr : %u",ptr);

 return 0;
}

Sample Output

Value inside ptr : 100 (if)
Value inside ptr : 120 

One should always remember that only integer can be added to a pointer variable. But for subtraction we can subtract one pointer from another if they are of same type.

2. Difference & Comparison

Two pointer can be subtracted if they hold same memory space and same type.

ptr2 - ptr1 = (Address of ptr2 - Addres of ptr1) / sizeof(*ptr1 or *ptr2)

Comparison of two pointer is perfectly valid in C even if they hold different type of variable, see the following example

int main()
{
 int *ptr1;
 float *ptr2;

 ptr1 = (int *)1;
 ptr2 = (float *)2;

 if(ptr2 > ptr1)
   printf("Ptr2 is far from ptr1");

 return(0);
}

3. Invalid operations

Following operations are invalid operations with pointers
1. Addition of two pointers
2. Multiplication of two pointers/one pointer and one variable(int/float etc)
3. Division between two pointers/One pointer and one variable (int/float etc)
4. Modulo operation on any pointer
5. Any Bitwise operation, negate operation

READ MORE
...