Pointer Practice Question in C

 Pointer Questions:

Q: Guess the output?


#include <stdio.h>

// Pointer
int main(){
    int *ptr;
    int x;

    ptr = &x;
    *ptr = 0;
    printf("x = %d\n", x); // 0
    printf("*ptr = %d\n", *ptr); // 0

    *ptr += 5;
    printf("x = %d\n", x); // 5
    printf("*ptr = %d\n", *ptr); // 5

    (*ptr)++;
    printf("x = %d\n", x); // 6
    printf("*ptr = %d\n", *ptr); // 6
    return 0;
}

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


#include <stdio.h>

int main() {
    int x = 34;
    printf("THe address of x is %u\n", &x);

    int* x_ptr = &x;
    printf("THe value of x is %d\n", *x_ptr);

    return 0;
}

Q: Write a program having a variable ‘i’. Print the address of ‘i’. Pass this variable to a function and print its address. Are these addresses same? Why?


#include <stdio.h>

void print_address(int*);

int main() {
    int i = 356;
    int* i_ptr = &i;
    printf("THe address of i is %u\n", i_ptr);
    print_address(i_ptr);

    return 0;
}

void print_address(int* i_ptr) {
    printf("THe address of i is %u\n", i_ptr);
}

/*
 Yes, the addresses printed inside and outside the function are the same.
 This is because i is the same variable, and you're passing its address to the
 function. The function operates on the address of the same memory location in
the stack.
*/

Q: Write a function to calculate the sum, product and average of two numbers. Print that average in the main function.


#include <stdio.h>

void doWork(int a, int b, int *sum, int *prod, int *avg);

int main(){
    int a = 5, b = 1;
    int sum, prod, avg;
    doWork(a, b, &sum, &prod, &avg);
    printf("sum = %d, prod = %d, avg =  %d", sum, prod, avg);
   
    return 0;
}

void doWork(int a, int b, int *sum, int *prod, int *avg){
    *sum = a + b;
    *prod = a * b;
    *avg = (a + b) / 2;
}

Q: Write a program using a function which calculates the sum and average of two numbers. Use pointers and print the values of sum and average in main().


#include <stdio.h>

float calc_sum(int*, int*);
float calc_avg(int*, int*);

int main() {
    int a = 5, b = 5;
    int* a_ptr = &a;
    int* b_ptr = &b;
    printf("Sum : %2f\n", calc_sum(a_ptr, b_ptr));
    printf("Avarge : %2f\n", calc_avg(a_ptr, b_ptr));

    return 0;
}

float calc_sum(int* x_ptr, int* y_ptr) {
    return *x_ptr + *y_ptr;
}

float calc_avg(int* x_ptr, int* y_ptr) {
    return (*x_ptr + *y_ptr) / 2;
}

Q: Write a program in C to find the maximum number between two numbers using a pointer?


#include <stdio.h>

void findMax(char *num1, char *num2, char *max);

// Any question of Pointer that means to create a function, Okay

int main(){
    int a, b, max;
    printf("Enter two numbers: ");
    scanf("%d %d", &a,&b);

    char *ptr1 = &a;
    char *ptr2 = &b;

    // Best
    findMax(ptr1, ptr2, &max);
    // OR
    // if(*ptr1 > *ptr2){
    //     max = *ptr1;
    // }
    // else{
    //     max = *ptr2;
    // }
    //  OR
    // if(&a > &b){
    //     max = a;
    // }
    // else{
    //     max = b;
    // }

    printf("Max number is %d", max);
    return 0;
}

void findMax(char *num1, char *num2, char *max){
    if(*num1 > *num2){
        *max = *num1;
    }
    else{
        *max = *num2;
    }
}

Q: Write a program to change the value of a variable to ten times of its current value using functions from pointer.


#include <stdio.h>

void change_value(int*);

int main() {
    int i = 23;
    int* ptr = &i;
    printf("THe address of i is %u\n", *ptr);
    change_value(ptr);
    printf("THe address of i is %u\n", *ptr);

    return 0;
}

void change_value(int* ptr) {
    *ptr *= 10;
}

Q: Write a function and pass the value by reference.


#include <stdio.h>

void change_value(int*);

int main() {
    int i = 56;
    int* ptr = &i;
    printf("THe address of i is %u\n", *ptr);
    change_value(ptr);

    return 0;
}

void change_value(int* ptr) {
    printf("THe address of i is %u\n", *ptr);
}

Q: Write a program to print the value of a variable i by using “pointer to pointer” type of variable.


#include <stdio.h>

int main() {
    int i = 39;
    int* ptr = &i;
    int** pptr = &ptr;
    printf("The value of i is %d", **pptr);

    return 0;
}

Q: Write a program in C to print the elements of an array in reverse order?


Q: Write a program in C to print all the letters in English alphabet using a pointer?


#include <stdio.h>

void printAlphabets(char *firstPtr, char *lastPtr);

int main(){
    char firstLetter = 'a', lastLetter = 'z';
    char *firstPtr = &firstLetter;
    char *lastPtr = &lastLetter;

    printAlphabets(firstPtr, lastPtr);
   
    return 0;
}

// Option 1
// void printAlphabets(char *firstPtr, char *lastPtr){
//     for(char *l = firstPtr; *l <= *lastPtr; (*l)++){
//         printf("%c ", *l);
//     }
// }

// Option 2
void printAlphabets(char *firstPtr, char *lastPtr){
    for(char l = *firstPtr; l <= *lastPtr; l++){
        printf("%c ", l);
    }
}

Post a Comment

0 Comments