Pointers in C

C provides the important feature of data manipulations with the address of the variables, the execution time is very much reduced such concept is possible with the special data type called Pointers.

Pointer:- A pointer is a variable which stores the address of another variable.

Declaration:-Pointer declaration is similar to normal variable declaration butpreceded by a *

Syntax:- data type  *identifier;

Example:- int *p;

Initialization:- datatype *identifier=address;

 Example:- int n;

                                                                  int *p=&n;

NULL:-NULL  pointer value(empty address)

                            Any type of pointer allocates two bytes of memory because it stores address of memory allocation.In c language the programme keep ([memory]) size is 64 kilo bytes.It is in unsigned integer range.

NOTE:-  Any type of pointer it allocates 2 bytes memory. Because it stores address of memory  location.

#include<stdio.h>
int main()
{
int ptr, q; q = 50; / address of q is assigned to ptr / ptr = &q; / display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
POINTERS IN C – PROGRAM OUTPUT:
50
KEY POINTS TO REMEMBER ABOUT POINTERS IN C:
  • Normal variable stores the value whereas pointer variable stores the address of the variable.
  • The content of the C pointer always be a whole number i.e. address.
  • Always C pointer is initialized to null, i.e. int *p = null.
  • The value of null pointer is 0.
  • & symbol is used to get the address of the variable.
  • * symbol is used to get the value of the variable that the pointer is pointing to.
  • If a pointer in C is assigned to NULL, it means it is pointing to nothing.
  • Two pointers can be subtracted to know how many elements are available between these two pointers.
  • But, Pointer addition, multiplication, division are not allowed.
  • The size of any pointer is 2 byte (for 16 bit compiler).

Void *:-

It is a generic pointer. It refers the address of any type of variable and also it will assigned to any type of pointer.

Example:-   int n=100;

                                                                    int *p=&n;

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
  int n;
  int p=&n; 
  clrscr();
  printf("Enter any value into n:"); 
  scanf("%d",p); //scanf("%d",&n); Here in pointers& is not necessary 
  printf("Given number in through pointer:%d",p);
  getch();
}

FUNCTIONS AND POINTERS

Call by value:-The process of passing a value to the function call is known as call by value.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
  void disp(int);
  int n;
  clrscr();
  printf("Enter any number:");
  scanf("%d",&n);
  disp(n);
  getch();
}
void disp (int x)
{
  clrscr();
  printf("%5d",x);
}

Call by Reference:The process of calling a function using pointers to pass the address of the variables is known as call by reference.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
void disp(int *);
int n;
clrscr();
disp(&n);
printf("%5d",n);
getch();
}
void disp( int *x)
{
printf("Enter any value into n:");
scanf("%d",x);
}
Program: Swapping two values
#include<stdio.h>
#include<conio.h>
void main()
{
  void swap(int *, int *);
  int a,b;
  clrscr();
  printf("Enter any two values:");
  scanf("%d%d",&a,&b);
  printf("\nBefore swaping:");
  printf("\na=%d",a);
  printf("\nb=%d",b);
  swap(&a,&b);
  printf("\nAfter swaping:");
  printf("\na=%d",a);
  printf("\nb=%d",b);
  getch();
}
void swap(int *x, int *y)
{
  int t;
  t=*x;
  *x=*y;
  *y=t;
}

POINTERS  AND  ARRAYS

When an array is declared, the compiler allocates a BASE address and sufficient amount of storage and contain all the elements of array in continuous memory allocation.

The base address is the location of the first element (index 0 of the array).The compiler also defines the array name as a constant pointer pointed to the first element.

suppose we declare an array ‘a’ as follows.

Example:- int a[5]={1,2,3,4,5};

Suppose the base address of a is 1000 and assuming that each integer requires 2 bytes. Then the 5 elements will be stored as follows.

    Elements——>  a[0] a[1] a[2] a[3] a[4]

                                   ——————————

    values——->          1     2     3     4       5

                                   —————————–

    address——>   1000 1002 1004 1006 1008

                             base address

Here ‘a’ is a pointer that points to the first element. so the value of a is 1000 there fore  a=&a[0]=1000

If we declare ‘p’ is an integer pointer, then we can make the pointerp to point to the array ‘a’ by the following assignment.

int *p;

        p=&a[0]=a;

Now we can access every value of a using p++(or)p+1 to move one element to another.

The relationship between p and a is shown below.

p+0 = &a[0]=1000

p+1 = &a[1]=1002

p+2 = &a[2]=1004

p+3 = &a[3]=1006

p+4 = &a[4]=1008

When handling arrays instead of using array indexing, we can use pointer to access array elements. Note that *(p+k) gives the value of a[k]. Pointer accessing method is much faster than array indexing.

Program: Write a program to accept and display array elements using pointers
#include<stdio.h>
#include<conio.h>
void main()
{
  int a[20],n,i;
  int p=&a[0]; 
  clrscr(); 
  printf("Enter no of elements:"); 
  scanf("%d",&n); 
  printf("Enter array elements:"); 
  for(i=0;i<n;i++)
  {
    scanf("%5d",p+i);
  }
  printf("Given array elements:");
  for(i=0;i<n;i++)
  {
    printf("%5d",*(p+i));
  }
  getch();
}

Follow Us On