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