I recently came across this question in an Interview process. I need some help to understand the logic behind 2nd output of this program.
#include <stdio.h>
char *c[] = {"GeksQuiz", "MCQ", "TEST", "QUIZ"};
char **cp[] = {c+3, c+2, c+1, c};
char ***cpp = cp;
int main()
{
printf("%s ", **++cpp); //1st statement
printf("%s ", *--*++cpp+3); //2nd statement
printf("%s ", *cpp[-2]+3); //3rd statement
printf("%s ", cpp[-1][-1]+1); //4th statement
return 0;
}
output:- TEST sQuiz Z CQ
What I understand from above code:
for the sake of simplicity we can consider cp[] as {QUIZ TEST MCQ GeksQuiz}
1st statement:
**++cpp -> cpp will points to base address of TEST and dereferencing it 2 times gives TEST which is fine.
but in 2nd statement I can't demystify the logic:
*--*++cpp+3 -> ++cpp points to MCQ *++cpp will be the address of M , --*++cpp will be previous address to M, now I'm stuck here. how it's getting sQuiz as output?
(afaik ++(suffix) and * have same precedence and right to left associativity)
(DISCLAIMER: please broaden your mind. not all codes are meant for product development. this code evaluates the understanding of C pointers)