Discussion
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.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development