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

Discussion

What will be the output of the program ?

#include <stdio.h>

int main()
{
    static char *s[] = {"black", "white", "pink", "violet"};
    char **ptr[] = {s+3, s+2, s+1, s}, ***p;
    p = ptr;
    ++p;
    printf("%s", **p+1);
    return 0;
}

 

  • A.ink
  • B.ack
  • C.ite
  • D.let

Answer: A

Correct answer: ink

Work outward. ptr holds {s+3, s+2, s+1, s}, and p = ptr then ++p leaves p pointing at ptr[1], which is s+2.

So *p is s+2, and **p is s[2], the pointer to "pink". Finally **p+1 advances that char pointer one position, so printf starts from the second character and prints ink. The trap is applying the +1 at the wrong level - it binds to the char pointer, not to p.

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

Post your comment

Please log in or register to post a comment.