Practical List – 3

 

Practical List – 3

 

 

1. WAP to print address of variable.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    clrscr();

    int number = 5, *ptr;

    ptr = &number;

 

    printf("Address of number: %u", ptr);

    printf("\nValue of number: %u", *ptr);

    printf("\nAddress of ptr: %u", &ptr);

    getch();

}

 

Output:

Address of number: 2856320204

Value of number: 5       

Address of ptr: 2856320192

 

 

 

2. WAP to access element using pointer.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    clrscr();

    int arr[] = {10, 20, 30, 40, 50};

    int *ptr;

 

    ptr = &arr[0];

 

    for (int i = 0; i < 5; i++)

    {

        printf("\narr[%d] = %d", i, *(ptr + i));

    }

    getch();

}

 

Output:

 

arr[0] = 10

arr[1] = 20

arr[2] = 30

arr[3] = 40

arr[4] = 50

 

 

 

Please follow me for more practicals....


3. WAP to read & print array elements using pointer.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    clrscr();

    int arr[5], i;

 

    int *ptr;

    ptr = &arr[0];

 

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

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

    {

        printf("Enter arr[%d] = ", i);

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

    }

 

    printf("\nPrinting array elements:");

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

    {

        printf("\narr[%d] = %d", i, *(ptr + i));

    }

    getch();

}

 

Output:

Enter the array elements:

Enter arr[0] = 10

Enter arr[1] = 20

Enter arr[2] = 30

Enter arr[3] = 40

Enter arr[4] = 50

 

Printing array elements:

arr[0] = 10

arr[1] = 20

arr[2] = 30

arr[3] = 40

arr[4] = 50

 

 

 

 

4. WAP for pointer to pointer.

Program:

#include <stdio.h>

#include <conio.h>

int main()

{

    clrscr();

    int a, *ptr1, **ptr2, ***ptr3;

 

    ptr1 = &a, ptr2 = &ptr1, ptr3 = &ptr2;

 

    printf("\nAddress of normal variable 'a' = %u\n", ptr1);

    printf("Address of pointer variable '*ptr1' = %u\n", ptr2);

    printf("Address of pointer-to-pointer '**ptr2' = %u\n", ptr3);

    return 0;

}

 

Output:

Address of normal variable 'a' = 1293940644

Address of pointer variable '*ptr1' = 1293940632

Address of pointer-to-pointer '**ptr2' = 1293940624

 

 

 

Please follow me for more practicals....


 

5. WAP of void pointer.

Program:

#include <stdio.h>

#include <conio.h>

int main()

{

    clrscr();

    int iNum = 10;

    float fNum = 10.202022;

    void *ptr;

    ptr = &iNum;

    printf("\nValue of iNum = %d", *((int *)ptr));

    ptr = &fNum;

    printf("\nValue of fNum = %f", *((float *)ptr));

 

    return 0;

}

 

 

Output:

 

Value of iNum = 10

Value of fNum = 10.202022

 

 

 

 

6. WAP of null pointer.

Program:

#include <stdio.h>

#include <conio.h>

int main()

{

    clrscr();

    int *ptr = NULL;

 

    if (ptr == NULL)

    {

        printf("Pointer is NULL\n");

    }

    else

    {

        printf("Pointer is not NULL\n");

    }

    return 0;

}

 

Output:

Pointer is NULL

 

 

 

 

7. WAP of array of pointer.

Program:

#include <stdio.h>

#include <conio.h>

 

const int MAX = 3;

 

int main()

{

    clrscr();

    int arr[] = {10, 100, 200};

    int i, *ptr[MAX];

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

    {

        ptr[i] = &arr[i];

    }

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

    {

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

    }

    return 0;

}

 

Output:

Value of arr[0] = 10

Value of arr[1] = 100

Value of arr[2] = 200

 

 

Please follow me for more practicals....


Post a Comment

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