What will be the output of the program ?
#include <stdio.h>
int main()
{
char str[20] = "Hello";
char *const p=str;
*p='M';
printf("%s\n", str);
return 0;
}
Answer: A
Correct answer: Mello
char *const p makes the pointer const, not what it points to. So p cannot be made to point elsewhere, but *p = 'M' is perfectly legal and writes through it.
str is a modifiable array, so the write succeeds and the string becomes Mello. Compare const char *p, where the qualifier applies to the pointed-to characters and the assignment would be rejected - the position of const relative to the asterisk decides which is protected.
Please log in or register to report this question.