[英]What the following code prints / with arrays, pointers etc
I have problem solving this problem, so if anyne had a similar problem it would help me a lot.我在解决这个问题时遇到了问题,所以如果有人有类似的问题,它会对我有很大帮助。
short y[2][3]={{0123},{0x12345}},*p=y[1];
printf("01:%x\n", y);
printf("02:%x\n", p);
printf("03:%x\n", sizeof(y));
printf("04:%x\n", sizeof(y[0]));
printf("05:%x\n", sizeof(&y[0]));
printf("06:%x\n", sizeof(*p));
printf("07:%x\n", sizeof(p++));
printf("08:%x\n", *p++);
printf("09:%x\n", *p);
return 0;
Can anyone explain to me why the printout is like this?谁能给我解释一下为什么打印输出是这样的?
01:61ff10
02:61ff16
03:c
04:6
05:4
06:2
07:4
08:2345
09:0
My opinion:我的想法:
01:Prints the address where the array y begins.
02:Prints the address of the pointer, which points to the second element of the array. Since we have 2 * 3 elements that are of type short, each subsequent element of the zero element will increase by 6.
03:Since we have 2 * 3 elements, which is equal to 6, but the elements of the type are short, so it will print hexadecimal c
04:the number of elements in the zero position is 3, but they are of the short type, so it prints 6
05:prints the sizeof addresses of the first element of the array which is 4
06:I don't know why it prints 2 here
07:Prints the sizeof of the pointer address which is 4, it will increase after printing
08:I do not understand
09:I do not understand
Can anyone explain why it prints like this?谁能解释为什么它打印成这样?
OK, let's see:好吧,让我们来看看:
y
. y
的地址。p
, which holds the address of y[1]
, which is the second element of type short[3]
. p
的值,它包含y[1]
的地址,它是short[3]
类型的第二个元素。 The size of a short
is apparently 2 on your system, so the offset to #01 is 6.short
的大小显然是 2,因此 #01 的偏移量是 6。y
, 2 * 3 * sizeof (short)
give 12, in hex c
. y
的大小, 2 * 3 * sizeof (short)
给出 12,十六进制c
。y[0]
, which is of type short[3]
. y[0]
的大小,类型为short[3]
。 6, as you found. y[0]
, and apparently the size of an address is 4 on your system. y[0]
的地址大小,显然地址的大小在您的系统上是 4。p
points to. p
指向的object的大小。 This is a short
, so 2.short
的,所以 2。p++
, which is an address, so 4. And no, p
is not incremented, since the expression is not evaluated. p++
的大小,它是一个地址,所以是 4。不, p
没有递增,因为表达式没有被计算。p
points to, which is y[1][0]
. p
指向的object的值,即y[1][0]
。 Since the initializing value of 0x12345
is an int
too big to be stored in a short
, it is truncated to 0x2345
.0x12345
的初始值是一个太大而无法存储在short
中的int
,因此它被截断为0x2345
。 After reading the value, p
is incremented.p
递增。p
points to, which is y[1][1]
. p
指向的元素,即y[1][1]
。 It was initialized to 0. Notes:笔记:
You should have got warnings from your compiler:你应该从你的编译器那里得到警告:
%p
.%p
。sizeof
might not match the format %x
. sizeof
结果的类型可能与格式%x
不匹配。 You should take warnings seriously, they are always a hint that you most probably made an error.你应该认真对待警告,它们总是暗示你很可能犯了错误。
N6) Sizeof(*p) is size of datatype pointed by p. N6) Sizeof(*p) 是 p 指向的数据类型的大小。 p is pointer to short: so 2 bytes.
p 是指向 short 的指针:所以 2 个字节。
N8) p is pointer to short, it`s pointing to array element y[1][0]. N8) p 是指向 short 的指针,它指向数组元素 y[1][0]。 y[1] and y[1][0] have the same memory address.
y[1] 和 y[1][0] 具有相同的 memory 地址。
All array elements are short, 0x12345 truncates to 0x2345 upon array initialisation.所有数组元素都很短,0x12345 在数组初始化时截断为 0x2345。 So output is 2345. Also, p++ increases pointer value to point to next short y[1][1].
所以 output 是 2345。此外,p++ 增加指针值以指向下一个短 y[1][1]。
N9) Because of p++ in step N8, pointer now points to y[1][1], which was initialised to 0 (by default, because init value not provided) - output is 0. N9) 由于步骤 N8 中的 p++,指针现在指向 y[1][1],它被初始化为 0(默认情况下,因为未提供初始值)- output 为 0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.