Showing posts with label Pointers. Show all posts
Showing posts with label Pointers. Show all posts

01 February 2022

Pointer To Pointer

 QUESTION :

Write a Program to print the value of a variable 'i' by using "Pointer to Pointer" type variable.


#include<stdio.h>
 
int main()
{
    int i = 2;
    int *ptr;
    int **ptr_ptr;
    ptr = &i;
    ptr_ptr = &ptr;

    printf("The value of i is %d", **ptr_ptr);
 
return 0;
}

OUTPUT--->
The value of i is 2


Pointers

 QUESTION :

Write a program using a Function that calculates the Sum And Average of two numbers. 

Use pointers and print the value of Sum and Average in main ().


#include<stdio.h>

void sumAndAvg(int a, int b, int*sum, float*avg){
            *sum = a + b;
            *avg = (float) (*sum/2);
}
 
int main()
{
    int i,j,sum;
    float avg;
    i = 7;
    j = 3;

    sumAndAvg(i,j, &sum, &avg);
    printf("The sum of two numbers is %d\n", sum);
    printf("The average of two numbers is %f\n", avg);
 
return 0;
}

OUTPUT--->

The sum of two numbers is 10 The average of two numbers is 5.000000








31 January 2022

Print Address of variable Using Pointers

 Question :

Write a program to print the address of a variable. Use this address to get the value of this variable.

#include<stdio.h>
 
int main()
{
    int a = 111;
    int *ptr;
    ptr = &a ;
   
    printf("The address of variable a is : %u\n", &a);
    printf("The value of variable a is : %d\n", *(&a));
    printf("The value of variable a is : %d\n", *ptr);

 
return 0;
}

OUTPUT--->

The address of variable a is : 6422216 The value of variable a is : 111 The value of variable a is : 111