Wednesday, 13 September 2017

What is pointer? How is it initialized? What is indirection operator? What is the scale factor

Q : What is pointer? How is it initialized? What is indirection operator? What is the scale factor?

Solution: 


Pointer

A pointer is a special type of variable which hold physical address of a variable. * is used to make pointer. Programmer can change a variable value from another location with the help of pointer. Call reference is an example of pointer. In call by value context a variable physical address pass to a function and change made inside of function direct effect actual value of passed variable due to pointer.
& (Address operator ) is used to assign address of a variable to pointer. * (Indirection Operator) is used to access value of variable point by pointer.
 

Pointer C Language Program



200,300 is physical ad
dress
Syntax :
Data_type *var_name
e.g.
void main()
{
     int *ptr;
     int x=10;
     ptr=&x;
     printf("\nValue of x through pointer %d",*ptr);
}

indirection operator

The indirection operator (*) accesses a value indirectly, through a pointer. The operand must be a pointer value. The result of the operation is the value addressed by the operand; that is, the value at the address to which its operand points. The type of the result is the type that the operand addresses.

If the operand points to a function, the result is a function designator. If it points to a storage location, the result is an l-value designating the storage location.

If the pointer value is invalid, the result is undefined. The following list includes some of the most common conditions that invalidate a pointer value.

  • The pointer is a null pointer.
  • The pointer specifies the address of a local item that is not visible at the time of the reference.
  • The pointer specifies an address that is inappropriately aligned for the type of the object pointed to.
  • The pointer specifies an address not used by the executing program.


Scale factor


When add and subtract operator applied to primitive data type variable then variable will increase/decrease by one from its current value. This is the case of primitive type data type variable but not for pointer.

A pointer is a special data type which holds address of a memory location. When add or subtract operator has applied on pointer then it will not work primary data types variable does. It get move place forward or backward depend on operator. Moving means pointer will get next variable address. See the picture.

Scale factor C Language


As shown in picture it is an integer array so pointer will point 65365 at the time of declaration. When increment operator applied pointer will point 65367.

An integer pointer always jumps two places by increment or decrement because of size of integer. A list of scale move of 16 bit compiler is on following table.

Data Type
Jump in Byte
Int
2
Char
1
Float
4
Double
8
Long
4
Long double
10

Operator with scale


Only increment and decrement operator is allowed with a pointer for scaling.

#include<stdio.h>

void main()
{

     int ar[5],i,*pt;
     pt=ar;
     printf("\nAddress by addition\n\n");
     for(i=0;i<5;i++)
     {
          printf("\t[%u]",pt);
          pt++;
     }
     printf("\nAddress by subtraction\n\n");
     for(i=0,pt--;i<5;i++)
     {
          printf("\t[%u]",pt);
          pt--;
     }


}