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 non pointer variable.Symbol * is used to make a pointer variable. Programmer can change a variable value from another location with the help of pointer. Call reference is an example of pointer , in which  a variable physical address pass to a function and any change made inside of function direct affect 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 address
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 (*) is used to access  value of a variable indirectly through  pointer varable. The operand with * must be a pointer variable and result of the operation is the value point by the operand. 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--;
     }


}





No comments:

Post a Comment