Skip to content
Related Articles

Related Articles

Improve Article

C | Advanced Pointer | Question 5

  • Difficulty Level : Easy
  • Last Updated : 28 Jun, 2021
Geek Week

Predict the output




#include <string.h>
#include <stdio.h>
#include <stdlib.h>
  
void fun(char** str_ref)
{
    str_ref++;
}
  
int main()
{
    char *str = (void *)malloc(100*sizeof(char));
    strcpy(str, "GeeksQuiz");
    fun(&str);
    puts(str);
    free(str);
    return 0;
}

(A) GeeksQuiz
(B) eeksQuiz
(C) Garbage Value
(D) Compiler Error


Answer: (A)

Explanation: Note that str_ref is a local variable to fun(). When we do str_ref++, it only changes the local variable str_ref.

We can change str pointer using dereference operator *. For example, the following program prints “eeksQuiz”

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void fun(char** str_ref)
{
    (*str_ref)++;
}

int main()
{
    char *str = (void *)malloc(100*sizeof(char));
    strcpy(str, "GeeksQuiz");
    fun(&str);
    puts(str);
    free(str);
    return 0;
}


Quiz of this Question

Take a step-up from those “Hello World” programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.
My Personal Notes arrow_drop_up
Recommended Articles
Page :