Pointer in C Programming | Pointer Simplest | Null Pointer

What is a Pointer in C | Structure Pointer in C | Function Pointer in C

Pointer in C: A pointer is a variable that contain the address of another variable. we can say that the pointer "points to" another variable.

A pointer is a special type of variable which is used to store the address of another variable. It can be store the address of same data type means an integer pointer can be store the address of integer variable, Character pointer can store the address of character variable.

Pointer in C Programming | Pointer Simplest | Null Pointer
Pointer in C Programming | Pointer Simplest | Null Pointer

Pointer denoted by asterisk(*) symbol with any variable at the time of declaring variable then this variable is called pointer variable.

We can use ampersand symbol to get the address of the variable. asterisk(*) symbol is used to get the value at the address which is hold by pointer.

    Pointer in C syntax

    int a; -> normal Variable

    int *p;-> pointer variable


    int a=100;

    int *p;

    p=&a;


    Pointer in C Language with Example

    #include<stdio.h>

    #include<conio.h>

    int main()

    {

     int a=10;

    int *p;

    p=&a;


    printf("Value of a:%d\n",a);

    printf("Address of a:%u\n",&a);

    printf("Value of p:%d\n",p);

    printf("Address of p:%u\n",&p);

    printf("Value of *p:%d\n",*p);

    getch();

    }


    **********OUTPUT**********

    Value of a:10

    Address of a:8284

    Value of p:10

    Address of p:8284

    Value of *p:10


    Function Pointer in C Programming Language

    Function Pointer: Pointer is a function pointer is a pointer that points to the memory address of a function. You can use a function pointer to call a function indirectly, or to pass a function as an argument to another function.

    Function Pointer in C syntax

    return_type (*ptr_name)(parameter_list);


    Function Pointer in C Language with Example

    #include<stdio.h>

    #include<conio.h>

    void area(int *h,int *w)

    {

      int  ar=*h * *w;

    printf("Area of Rectangle:%d",area);

    }

    void main()

    {

    int x,y;

    printf("Enter Height:");

    scanf("%d",&x);

    printf("Enter Width:");

    scanf("%d",&y);

    area(&x,&y);

    getch();

    }


    **********OUTPUT**********

    Enter Height:12

    Enter Width:13


    Area of Rectangle:156


    Pointer To Array in C Programming 

    Pointer To Array: Array pointer to an array is a variable that stores the memory address of the first element in the array. You can use a pointer to an array to access the elements of the array, either by dereferencing the pointer or by using pointer arithmetic.

    Array Pointer in C syntax

    data_type (*ptr_name)[num_elements];


    Array Pointer in C Language with Example

    #include<stdio.h>

    #include<conio.h>

    void main()

    {

    int a[10]={1,2,3,4,5,6,7,8,9};

    for(i=0;i<9;i++)

    {

       printf("%d ",*(a+i));

    }

    getch();

    }


    **********OUTPUT**********

    1 2 3 4 5 6 7 8 9


    Structure Pointer in C Programming

    Structure Pointer: A structure pointer is a variable that stores the memory address of a structure. You can use a structure pointer to access the members of the structure, either by dereferencing the pointer or by using the -> operator.

    Structure Pointer in C syntax

    struct struct_name *ptr_name;



    Structure Pointer in C Language with Example

    #include <stdio.h>

    #include<conio.h>

    struct student {

        char name[50];

        int roll;

        float marks;

    };


    void main() 

    {

        struct student s = {"Learn Coding", 1938, 80.5};

        struct student *p = &s;

        

        printf("Name: %s\n", p->name);

        printf("Roll number: %d\n", p->roll);

        printf("Marks: %.2f\n", p->marks);

        

        getch();

    }


    **********OUTPUT**********

    Name:Learn Coding

    Roll number: 1938

    Marks: 80.50


    Null Pointer in C Programming

    Null Pointer: A null pointer is a pointer that does not point to any memory location. It is represented by the NULL macro, which is defined in the standard header file <stddef.h>.

    A null pointer is often used to indicate the end of a data structure, or to initialize a pointer variable before it is assigned a valid memory address.

    Null Pointer in C Language with Example

    #include <stdio.h>

    #include<conio.h>

    #include <stddef.h>


    void main() 

    {

        int *ptr = NULL;

        

        if (ptr == NULL) 

    {

            printf("The pointer is null\n");

        } 

    else 

    {

            printf("The pointer is not null\n");

        }

        

        getch();

    }



    **********OUTPUT**********

    The pointer is null


    Post a Comment

    0 Comments