Add

Top 11 usefull Program for Pointers in c language

                     

Let's see the Top 11 usefull Program for Pointers  in c language.

Best Way To Learn C Programming free

Basic For Loop program in c language

Top 10 Basic C Programs For Beginners

1. Program for Pointers.

important points to remember:

* is used to access the value stored in the pointer variable.

& is used to store the address of a given variable.

  • simple program on pointer.

int *p; is a pointer variable declaration where p is a pointer to an int variable i.e. it stores the location of an integer.

%x is a format specifier to print hexadecimal value. It is usually used to print the location.

#include <stdio.h>

int main()             

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int var = 24;   // actual variable declaration

    int *p;

    p = &var;   // storing address of int variable var in pointer p

    printf("\n\nAddress of var variable is: %x \n\n", &var);

    // address stored in pointer variable

    printf("\n\nAddress stored in pointer variable p is: %x", p);

    // access the value using the pointer variable

    printf("\n\nValue of var variable or the value stored at address p is   %d ", *p);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

} 

2.Program for Dynamic Memory Allocation Program using malloc().

 Program on dynamic memory allocation using malloc() and clearing out memory space using free().

sizeof() returns the number of bytes occupied by any datatype, in this case by an integer.

#include <stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int n, i, *ptr, sum = 0;

    printf("\n\nEnter number of elements: ");

    scanf("%d", &n);

   // dynamic memory allocation using malloc()  

   ptr = (int *) malloc(n*sizeof(int));

   if(ptr == NULL) // if empty array

    {

        printf("\n\nError! Memory not allocated\n");

        return 0;   // end of program

    }

      printf("\n\nEnter elements of array: \n\n");

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

    {

        // storing elements at contiguous memory locations

        scanf("%d", ptr+i);   

        sum = sum + *(ptr + i);

    }

      // printing the array elements using pointer to the location

    printf("\n\nThe elements of the array are: ");

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

    {

        printf("%d  ",ptr[i]);    // ptr[i] is same as *(ptr + i)

    }

     /*

        freeing memory of ptr allocated by malloc

        using the free() method

    */

    free(ptr);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

3.Program to access Array of int Pointers.

#include <stdio.h>

/*

    Global declaration.

    Value of a const variable cannot be changed

    throughout the execution of program

*/

const int MAX = 5; 

int main()

{

 printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

 int var[]={10, 20, 30, 40, 50}; // initializing an array(here var) of int pointers

    int i = 0;

 /*

        ptr is an array of int pointers i.e.

        it stores the address of each array element

    */

    int *ptr[MAX];

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

    {

        /*

            Assign the address of each of the array

            element to the ptr array

        */

        ptr[i] = &var[i];

    }

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

    {

        /*

            ptr[i] stores the address of the element var[i].

            Hence, *ptr[i] returns the value of the element

            stored at location ptr[i]

        */

        printf("Value of var[%d] = %i\n\n", i, *ptr[i]);

    }

 printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

4.Accessing array elements(Traversing array) by incrementing a Pointer.

Name of the array refers to the base address of the array.

Here we have a tutorial to understand How Pointer arithmetic works?

Below is a program to access elements of an array using pointer increment.

#include <stdio.h>

const int MAX = 3;  // Global declaration

int main()

{

            printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

            int var[] = {100, 200, 300};

    int i, *ptr;

    /*

        storing address of the first element

        of the array in pointer variable

    */

    ptr = var;

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

    {

        printf("\n\n\nAddress of var[%d] = %x ", i, ptr);

        printf("\nValue of var[%d] = %d ", i, *ptr);

       // move to the next location

        ptr++;

    }

            printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

5.Pointer Comparison.

Program on pointer comparison for same type of pointer:

#include <stdio.h>

int main()

{

    int *ptrA,*ptrB;

    ptrA = (int *)1;

    ptrB = (int *)2;

        if(ptr2 > ptr1)

        printf("PtrB is greater than ptrA");

return(0);

}

6.Program for Pointer to a Pointer.

 Int var; is a integer variable which stores value.

int *ptr; is a pointer variable which stores the address of an integer variable.

int **pptr; is a pointer to a pointer variable which stores the address of a pointer variable.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int var;

    int *ptr;

    int **pptr;

    var = 50;

    // take the address of the variable var

    ptr = &var;

    // taking the address of ptr using address of operator-&

    pptr = &ptr;

    // take the value using the pptr

    printf("\n\nValue of var = %d\n\n", var);

    printf("\n\nValue available at *ptr = %d\n\n", *ptr);

    printf("\n\nValue available at **pptr = %d\n\n", **pptr);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

7. Program to Concatenate Strings using Pointer.

 program to concatenate strings using pointer:

#include <stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    char aa[100], bb[100];

    printf("\nEnter the first string: ");

    gets(aa);   // inputting first string

 

    printf("\nEnter the second string to be concatenated: ");

    gets(bb);   // inputting second string

    char *a = aa;

    char *b = bb;

    // pointing to the end of the 1st string

    while(*a)   // till it doesn't point to NULL-till string is not empty

    {

        a++;    // point to the next letter of the string

    }

    while(*b)   // till second string is not empty

    {

        *a = *b;

        b++;

        a++;

    }

    *a = '\0';  // string must end with '\0'

    printf("\n\n\nThe string after concatenation is: %s ", aa);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

8. Program to Reverse a String using Pointer.

#include <stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    char str[100];

    char rev[100];

    char *sptr = str; // sptr stores the base address of the str

    char *rptr = rev; // rptr stores the base address of the reverse

    int i = -1;

    printf("\n\nEnter a string: ");

    scanf("%s", str);

    // storing the ending address of str in sptr

    while(*sptr)

    {

        sptr++;

        i++; // i is the index of the end location

    }

    // storing the string str in rev in reverse order

    while(i >= 0)

    {

        /*

            First decrementing then using as it stores

            the location after the end location due to above while loop

        */

        sptr--;

        *rptr = *sptr;  // storing the value in sptr in rptr

        rptr++; // pointing to next location

        i--;    // decrementing the index

    }

    /*

        String should always end with '\0' so explicitly

        putting it at the end of the string

    */

    *rptr = '\0';

    rptr = rev; // restoring the base address of the reverse string

    // storing the reverse string in the original string

    while(*rptr)

    {

        *sptr = *rptr;

        sptr++;

        rptr++;

    }

   // printing the reverse string

    printf("\n\nReverse of the string is: %s ", str);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

9.Program to Swap Two Numbers using Pointers.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

   int a, b;

    int *ptra, *ptrb;

    int temp;

   printf("Enter value for a: ");

    scanf("%d", &a);

    printf("\n\nEnter value for b: ");

    scanf("%d", &b);

   printf("\n\nThe values before swapping are: a = %d     b = %d", a, b);

   ptra = &a;    // to store the location of a

    ptrb = &b;    // to store the location of b

    temp = *ptra;   // temp stores the value at location ptra

    *ptra = *ptrb;  // assigning value at location  ptrb to ptra

    *ptrb = temp;   // assign value of themp to  the variable at location ptrb

    printf("\n\nThe values after swapping are: a = %d    b = %d", a, b);

    printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

10. program for Pointer to a Function

#include<stdio.h>

int func(int a, int b)  // function definition

{

    printf("\n\n a = %d \n", a);

    printf("\n\n b = %d \n", b);

}

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

   // function pointer

    int(*fptr)(int , int);

  // assign address to function pointer

    fptr = func;

  // function calling

    func(2,3);

    fptr(2,3);  // calling a function referring to pointer to a function

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

11.Null Pointer Program.

Null pointer is a special reserved value of a pointer. A pointer of any type has this reserved value. Formally, each specific pointer type(int *, char *, etc) has its own dedicated null-pointer value. Conceptually, when a pointer has that Null value it is not pointing anywhere.

Void pointer is a specific pointer type. void * which is a pointer that points to some data location in storage, which doesn't have any specific type.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int *ptr = NULL;    // ptr is a NULL pointer

    printf("\n\n The value of ptr is: %x ", ptr);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.