Output of C Programs | Set 12
Predict the output of below programs.
Inside fun(), pointer str2 is initialized as str1 and str1 is moved till ‘\0’ is reached (note ; after while loop). So str1 will be incremented by 13 (assuming that char takes 1 byte).
Inside fun(), q is a copy of the pointer p. So if we change q to point something else then p remains unaffected.
Note that we are passing address of p to fun(). p in fun() is actually a pointer to p in main() and we are changing value at p in fun(). So p of main is changed to point q of fun(). To understand it better, let us rename p in fun() to p_ref or ptr_to_p
Also, note that the program won’t cause any problem because q is a static variable. Static variables exist in memory even after functions return. For an auto variable, we might have seen some weird output because auto variable may not exist in memory after functions return.
Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.