Pointer (program & theory)

Pointer: is a special kind of variable that store the address of another variable nothing else, using pointer we can use the value of a variable without effecting its value, with pointer one can print the address and actual value at that adress

pointer are of many types
1. array pointer: used to store the address of whole array
2. simple pointer: used to store the address of a sigle variable
3. double pointer: used to point for pointer and its values
4. null pointer: declare  to make use  of it later
5. constant pointer: contant pointer

Program to use the concept of array pointer and a simple pointer

#include<conio.h>
#include<stdio.h>
void main()
{
    int a[6]={1,2,3,4,5,6};
    int (*p)[6];
    p=&a;
    int *b=&a[0];
    printf("%d",p);
    printf("\n %d",b);
    p++;
    b++;
    printf("\n %d",p);
    printf("\n %d",b);
}

Ouput:










Program to use constant Pointer

#include<conio.h>
#include<stdio.h>
void  main()
{
   
    int c=9;
    int v=0;
    int *const a=&c;
    printf("%d",*a);
    // a=&v;  This will through error.
}

Ouput:








Program to illustrate the concept of double pointer

#include<conio.h>
#include<stdio.h>
void  main()
{
    int *a,**b;
    int c=4;
    a=&c;
    printf(" a=%d",a);
    // *a=&c;   this will not work.
    //**b=a;this will not work.
    //**b=&a;this will not work.
    // **b=c;
    //printf(" **b=%d",**b);
    b=&a;
    printf("\nb=%d",b);//own address
    printf("b=%d",*b);//step 1 adress means (&*a)
    printf(" b=%d",**b);//step 2 value mens (c)
}

Ouput:






No comments:

Post a Comment