简体   繁体   English

C 指针:如何生成以下输出?

[英]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?有人可以解释第 3 行和第 4 行的输出吗? I understand the output of the first two printf statements but line 3 and 4 blew my mind!我理解前两个printf语句的输出,但第 3 行和第 4 行让我大吃一惊!

cpp points to cp at the begin. cpp在开始时指向cp 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 .它在前 2 行中增加了两次,因此它在第 3 行指向cp + 2 (cp + 2)[-2]*(cp + 2 - 2) ,也就是*cp *cp is c+3 , which is with another * "QUIZ" . *cpc+3 ,它与另一个* "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" . cpp仍然指向第 4 行的cp + 2 ,索引-1将给出cp[1] ,即c+2 ,再次索引-1给出c[1] ,即"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 .数组指示符cp被转换为指向由表达式c+3定义的第一个元素的指针。

In the calls of printf the pointer cpp is incremented twice.在 printf 的调用中,指针cpp增加了两次。 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 .因此,在 increment 之后的第一个调用指针 cpp 指向数组cp的第二个元素,即它指向像c+2一样初始化的元素。 So dereferencing the pointer we get the pointer to the string literal "TEST" that is outputted.因此,取消引用指针,我们得到指向输出的字符串文字"TEST"的指针。

In the second call the pointer cpp is again incremented.在第二次调用中,指针cpp再次增加。 So its points to the element of the array cp that is defined by the expression c + 1 .所以它指向由表达式c + 1定义的数组cp的元素。 Deereferencing this pointer *++cpp we get a pointer with the value c + 1 .取消引用这个指针*++cpp我们得到一个值为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 .递减值--*++cpp我们得到一个值为c的指针,该指针指向数组c的第一个元素。 Dereferencing the pointer we get the pointer to the first element of the string literal "GeksQuiz" .取消引用指针,我们得到指向字符串文字"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.现在将数字 4 添加到指针*--*++cpp+3我们得到一个指向字符串文字的第四个字符的指针。 So in this call of printf the output is "sQuiz"所以在 printf 的这个调用中,输出是"sQuiz"

As it was pointed out the pointer cpp points now to the third element of the array cp after incrementing it two times.正如指出的那样,指针 cpp 现在在将其递增两次后指向数组 cp 的第三个元素。 So the expression cpp[-2] yields the first element of attay cp that has the value c+3 .因此,表达式cpp[-2]产生 attay cp的第一个元素,其值为c+3 SO the literal "QUIZ" is outputted in this call所以在这个调用中输出了文字"QUIZ"

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 .此表达式cpp[-1]产生数组cp的第二个元素,其初始化方式类似于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使用表达式cpp[-1][-1]我们得到一个指向数组 c 的第二个元素的指针,该元素指向在此调用中输出的字符串文字"MCQ"

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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