Discussion
What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 11;
int *p = &i;
foo(&p);
printf("%d ", *p);
}
void foo(int *const *p)
{
int j = 10;
*p = &j;
printf("%d ", **p);
}
Answer: A
Correct answer: Compile time error
Read the parameter from the identifier outwards: p is a pointer to a const pointer to int. So *p - the pointer being pointed at - is qualified const, and *p = &j attempts to modify it.
The compiler rejects the assignment. The companion question uses int **const p, where the const attaches to p itself and *p remains modifiable - the same two words in the other order giving the opposite permission.
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