c - logic applied to pointer to string array -


i came across question in interview process. need understand logic behind 2nd output of 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 understand above code:

for sake of simplicity can consider cp[] {quiz test mcq geksquiz}
1st statement:
**++cpp -> cpp points base address of test , dereferencing 2 times gives test fine.

but in 2nd statement can't demystify logic:
*--*++cpp+3 -> ++cpp points mcq *++cpp address of m , --*++cpp previous address m, i'm stuck here. how it's getting squiz output?
(afaik ++(suffix) , * have same precedence , right left associativity)

(disclaimer: please broaden mind. not codes meant product development. code evaluates understanding of c pointers)

after first printf, cpp pointing c+2.

                 cp               +------+                                                                                    |      |                                                                                  0 |  c+3 +----------------------------------------------------------------+                   |      |                                                                   |                   |      |                                                                |                   +------+                                                                |                   |      |                                                                |                 1 |  c+2 +-------------------------------------------------+              |     cpp --------> |      |                                                 |              |                   |      |                                                 |              |                   +------+                                                 |              |                   |      +---------------------------------------+         |              |                 2 |  c+1 |                                       |         |              |                   |      |                                       |         |              |                   |      |                                       |         |              |                   +------+                                       |         |              |                   |      +-----------------------+               |         |              |                 3 |  c   |                       v               v         v              v                   |      |                                                                                    |      |                +-------------+------------+-----------+------------+               +------+             c  |  "geksquiz" | "mcq"      | "test"    | "quiz"     |                                              |             |            |           |            |                                       +-------------+------------+-----------+------------+                                         0             1           2            3        

in second printf, ++cpp increment cpp c+1.
*++cpp dereference cpp , give c+1.
-- decrement c+1 1 therefore, *--*++cpp give c.
c+3 point 4th character of "geksquiz", i.e. s.
note after second printf cpp point cp[0] pointing c now.

              +------+                                                                                    |      |                                                                                  0 |  c+3 +----------------------------------------------------------------+                   |      |                                                                   |                   |      |                                                                |                   +------+                                                                |                   |      |                                                                |                 1 |  c+2 +-------------------------------------------------+              |                   |      |                                                 |              |                   |      |                                                 |              |                   +------+                                                 |              |                   |      +-------------------------+                       |              |                 2 |  c   |                         |                       |              |      cpp -------->|      |                         |                       |              |                   |      |                         |                       |              |                   +------+                         |                       |              |                                                                 |      +-----------------------+ |                       |              |                 3 |  c   |                       v v                       v              v                   |      |                                                                                    |      |                +-------------+------------+-----------+------------+               +------+                |  "geksquiz" | "mcq"      | "test"    | "quiz"     |                                       |             |            |           |            |                                       +-------------+------------+-----------+------------+                                         0             1           2            3        

Popular posts from this blog