char和int中的**(ptr + 1)值的差 [英] Difference of value of **(ptr+1) in char and int

查看:69
本文介绍了char和int中的**(ptr + 1)值的差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我解决以下代码输出为何不同的问题:

Please help me out as to why in below code outputs are different:

int z[][3] = { 1, 2, 3, 4, 5, 6 };
printf("\n**(z+1): %d", **(z + 1));

输出:(z + 1):4

char y[][3] = { "A", "F", "G", "J", "M", "P" };
printf("\n**(y+1): %c", **(y+1));

输出:(y + 1):F

为什么在以上两个输出中,首先检查第四个索引,而在第二个输出中显示第二个索引?

Why in the above two outputs, first is checking the 4th index while in second output prints 2nd index ?

推荐答案


为什么在以上两个输出中,第一个检查第四个索引,而在第二个输出中打印第二个索引?

Why in the above two outputs, first is checking the 4th index while in second output prints 2nd index ?

这实际上并不能描述正在发生的事情。

That's not actually close to describing what is happening.

要了解正在发生的事情,请将示例写成它们的实际含义

To understand what is happening, write the examples into their actual meaning

int z[][3] = { 1, 2, 3, 4, 5, 6 };
printf("\n**(z+1): %d", **(z + 1));

实际上是

int z[][3] = { {1, 2, 3}, {4, 5, 6} };
printf("\n**(z+1): %d", **(z + 1));

其中, z [0] 是初始化的三个元素的数组其中 {1、2、3} z [1] 是由<$ c初始化的三个元素的数组$ c> {4,5,6} 。

where z[0] is an array of three elements initialised with {1, 2, 3} and z[1] is an array of three elements initialised with {4,5,6}.

在此 z + 1 等于& z [0] + 1 等于& z [1] (数组的地址)三个 int )。因此 *(z + 1)是(对) z [1] (由三个元素组成的数组)的引用而 **(z + 1) z [1] [0] 。由于 z [1] 是一个初始化为元素 {4,5,6} 的数组,因此 z [1] [0] 是该数组的第一个元素。其值为 4

In this z + 1 is equal to &z[0] + 1 which is equal to &z[1] (the address of an array of three int). So *(z+1) is (a reference to) z[1] (an array of three elements) and **(z+1) is z[1][0]. Since z[1] is an array initialised as elements {4,5,6}, z[1][0] is the first element of that array. This has a value of 4.

相比之下,

char y[][3] = { "A", "F", "G", "J", "M", "P" };
printf("\n**(y+1): %c", **(y+1));

每个字符串文字都被初始化为两个元素的数组,例如 A 初始化为 {'A','\0'}

each of the string literals is initialised as an array of two elements e.g. "A" is initialised as {'A', '\0'}.

现在 y 是三个 char 的数组。如果给三个元素的数组一个带有两个 char 的初始化程序,如此处所示,则未显式初始化的值将被初始化为零。所以

Now y is an array of arrays of three char. If an array of three elements is given an initialiser with two char, as is the case here, the values that are not explicitly initialialised are zero-initialised. So

char y[][3] = { "A", "F", "G", "J", "M", "P" };

等同于

char y[][3] = { {'A', '\0', '\0'}, {'F', '\0', '\0'}, {'G', '\0', '\0'}, {'J', '\0', '\0'}, {'M', '\0', '\0'}, {'P', '\0', '\0'}};

所以 y 是六个元素的数组,每个元素是数组,每个数组包含三个 char

So y is array of six elements, each of which is an array of three char.

使用与讨论 z 以上, y +1 等于& y [1] 其中, y [1] 是三个 char 的数组,它们初始化为 {'F','\0' , \0}
因此, *(y + 1)是(引用) y [1] ,而 **(y + 1) y [1] [0] 。其值为'F'

Using the same logic as in the discussion of z above, y + 1 is equal to &y[1] where y[1] is an array of three char that is initialized as {'F', '\0', '\0'}. So *(y + 1) is (a reference to) y[1], and **(y + 1) is y[1][0]. This has a value of 'F'.

这篇关于char和int中的**(ptr + 1)值的差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆