Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the output of this C code?

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

 

  • A.Compile time error
  • B.10   10
  • C.Undefined behaviour
  • D.10   11

Answer: A

Correct answer: Compile time error

int *const *p means a pointer to a constant pointer to int, so *p is const and cannot be assigned to. The line *p = &j therefore violates the constraint and the compiler rejects it. Reading such declarations from the name outward is the reliable method: p, pointer to, const, pointer to, int.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.