简体   繁体   中英

C Pointers: How is the following output genrated?

#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 \n", **++cpp);
    printf("%s \n", *--*++cpp+3);
    printf("%s \n", *cpp[-2]);      // line 3
    printf("%s \n", cpp[-1][-1]);   // line 4
    return 0;
}

Output:

TEST 
sQuiz 
QUIZ 
MCQ 

Can someone please explain the output of line 3 and line 4? I understand the output of the first two printf statements but line 3 and 4 blew my mind!

cpp points to cp at the begin. It gets incremented twice in the first 2 lines, therefore it points to cp + 2 at line 3. (cp + 2)[-2] is *(cp + 2 - 2) , which is *cp . *cp is c+3 , which is with another * "QUIZ" .

cpp still points to cp + 2 at line 4, with index -1 will give cp[1] , which is c+2 , again index -1 gives c[1] , which is "MCQ" .

Array designators used in expressions with rare exceptions are converted to pointers to their first elements.

So in this declaration

char ***cpp = cp;

the array designator cp is converted to pointer to its first element that is defined by the expression c+3 .

In the calls of printf the pointer cpp is incremented twice. The first time in this call

printf("%s \n", **++cpp);

and the second time in this call

printf("%s \n", *--*++cpp+3);

So inside the first call the pointer cpp after increment points to the second element of the array cp that is it points to the element initialized like c+2 . So dereferencing the pointer we get the pointer to the string literal "TEST" that is outputted.

In the second call the pointer cpp is again incremented. So its points to the element of the array cp that is defined by the expression c + 1 . Deereferencing this pointer *++cpp we get a pointer with the value c + 1 . Decrementing the value --*++cpp we get a pointer with the value c that is a pointer that points to the first element of the array c . Dereferencing the pointer we get the pointer to the first element of the string literal "GeksQuiz" . Now adding the number 4 to the pointer *--*++cpp+3 we get a pointer that points to the fourth character of the string literal. So in this call of printf the output is "sQuiz"

As it was pointed out the pointer cpp points now to the third element of the array cp after incrementing it two times. So the expression cpp[-2] yields the first element of attay cp that has the value c+3 . SO the literal "QUIZ" is outputted in this call

printf("%s \n", *cpp[-2]);      // line 3

This expression cpp[-1] yields the second element of the array cp that is initialized like c+2 . Using the expression cpp[-1][-1] we get a pointer to to the second element of the array c that is to the string literal "MCQ" that is outputted in this call

printf("%s \n", cpp[-1][-1]);   // line 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM