C | Advanced Pointer | Question 5

Predict the output

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


(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



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.