使用的sizeof与数组和指针实验 [英] Experiments using sizeof with arrays and pointers
问题描述
有关程序:
#包括LT&;&stdio.h中GT;
INT主要(无效)
{ INT(*一)[2];
INT B〔5]; 的printf(的sizeof(int)的:%祖\\ N的sizeof(INT));
的printf(的sizeof(INT *):%祖\\ N的sizeof(INT *)); 的printf(的sizeof(B):%祖\\ N的sizeof(B));
的printf(的sizeof((INT *)B):%祖\\ N的sizeof((INT *)b)条);
输出(的sizeof(和b [0]):%祖\\ N的sizeof(和b [0])); 的printf(的sizeof(一):%祖\\ N的sizeof(一));
的printf(的sizeof(A [0]):%祖\\ N的sizeof(A [0]));
的printf(的sizeof(A [1]):%祖\\ N的sizeof(A [1])); 返回0;
}
输出是:
的sizeof(INT):4 - >事实1
的sizeof(INT *):8 - >事实2
的sizeof(B):20 - >情况1
的sizeof((INT *)B):8 - >案例2
的sizeof(和b [0]):8 - >案例3
的sizeof(一):8 - >案例4
的sizeof(A [0]):8 - >案例5
的sizeof(一个[1]):8 - >案例6
问题/意见(在个案顺序):
-
是区分1输出20,因为
B
被宣布为一个整数数组即INT []
?通过Fact1所确认返回以字节为块。难道不是吗? -
我猜铸造
B
到为int *
在这里所作的区别。在这里,B
是
考虑一个指针。我证实了这一点使用Fact2。对还是错? -
和b [0]
衰变为指针B
。输出与Fact2一致。 -
我预期的 16在这里,但我得到了8作为输出。我的结论是,这是因为
A
毕竟是一个指针和输出Fact2一致。我得到了类似的问题2输出 -
A [0]
为指针。输出与Fact2不谋而合 -
A [1]
为指针。输出与Fact2不谋而合
请回答问题并纠正我,如果任何的意见是错误的。
请回答问题,如果任何的意见指正
是错误的。
- 是病例1的输出
20
,因为B
被声明为阵列
INT
egers即为int []
?通过Fact1所确认返回以字节为块。难道不是吗?
块引用>是,结果显示了
的sizeof(INT [5])
。因此,从Fact1,尺寸为5 * 4
<醇开始=2>
我猜铸造 B
到为int *
在这里做的差别。在这里,B
被认为是一个指针。我证实了这一点使用Fact2。对还是错?
块引用>右键。但加入了更多的信息:
的sizeof
只须前pression的类型和它的不评估前pression(用于值)除非是VLA类型。
(从节 6.5.3.4本的的sizeof 的的http://www.open-std.org/JTC1/sc22/wg14/www/docs /n1256.pdf相对=nofollow> C99规格)由于您是在最后的结果将投,之后什么都没有关系。
<醇开始=3>
和b [0]
衰变为指针B
。输出与Fact2不谋而合。
块引用>没有,是的。
B类型[0]
是INT
因而类型的和b [0]
已为int *
(回忆一下[...]
结合比<$更紧C $ C>&安培; )。没有衰减。是的输出与Fact2不谋而合。
<醇开始=4>
我预期的16,但在这里我得到了8作为输出。我的结论是,这是因为一个是毕竟一个指针和输出Fact2重合。我得到了类似的输出问题2。
块引用>
A
为指针INT
的阵列2。因此,打印尺寸为指针(一个INT
阵列)。
INT(*一)[2];
声明A
为指针,数组2INT
。所以,你得到指针数组的大小
。要得到期望的结果(指针数组2的大小
INT
)的使用:为int * a [2];
INT(*一)[2];一个匿名
+ ---- + + ---- + ---- +
| A | -----&GT; |国际|国际|
+ ---- + + ---- + ---- +INT * B [2];b
+ ---- + ---- +
|为int * |为int * |
+ ---- + ---- +
B [0] B〔1]
A [0]
为指针。输出与Fact2
不谋而合
A [2]
为指针。输出与Fact2不谋而合
块引用>如前所述,
要使用A
是一个指向INT
的阵列2。因此,A [指数]
是一个数组2,如果INT
。因此,类型的A [0]
和A [1]
是阵列2INT
。所以输出2 * 4
从事实1
可能无关紧要这个答案,但A
未初始化和在EX pression使用它会导致未定义行为。虽然这是罚款的sizeof
要了解输出,让我们分析
的参数类型的sizeof
的printf(的sizeof(B):%祖\\ N的sizeof(B)); // INT [5]
的printf(的sizeof((INT *)B):%祖\\ N的sizeof((INT *)b)条); //为int *
输出(的sizeof(和b [0]):%祖\\ N的sizeof(和b [0])); //为int *的printf(的sizeof(一):%祖\\ N的sizeof(一)); // INT(*)[2]
的printf(的sizeof(A [0]):%祖\\ N的sizeof(A [0])); // INT [2]
的printf(的sizeof(A [1]):%祖\\ N的sizeof(A [1])); // INT [2]一个便携式程序(并非万无一失)确认看起来像类型:
断言(的sizeof(B)==的sizeof(INT [5]));
断言(的sizeof((INT *)二)==的sizeof(INT *));
断言(sizeof的(和b [0])== sizeof的为(int *));断言(的sizeof(一)==的sizeof(INT(*)[2]));
断言(sizeof的(一个[0])==的sizeof(中间体[2]));
断言(sizeof的(一个[1])==的sizeof(中间体[2]));For the program :
#include<stdio.h> int main(void) { int (*a)[2]; int b[5]; printf("sizeof(int) : %zu\n", sizeof(int)); printf("sizeof(int*) : %zu\n", sizeof(int*)); printf("sizeof(b) : %zu\n",sizeof(b)); printf("sizeof((int*)b) : %zu\n",sizeof((int*)b)); printf("sizeof(&b[0]) : %zu\n",sizeof(&b[0])); printf("sizeof(a) : %zu\n",sizeof(a)); printf("sizeof(a[0]) : %zu\n",sizeof(a[0])); printf("sizeof(a[1]) : %zu\n",sizeof(a[1])); return 0; }
Output is :
sizeof(int) : 4 -> Fact 1 sizeof(int*) : 8 -> Fact 2 sizeof(b) : 20 -> Case 1 sizeof((int*)b) : 8 -> Case 2 sizeof(&b[0]) : 8 -> Case 3 sizeof(a) : 8 -> Case 4 sizeof(a[0]) : 8 -> Case 5 sizeof(a[1]) : 8 -> Case 6
Questions/Observations (in Case Order ) :
Is Case 1 output 20 because
b
was declared as an array of integers ieint[]
? The total block in bytes is returned as confirmed by Fact1. Isn't it?I guess casting
b
toint*
made the difference here. Hereb
is considered a pointer. I confirmed this using Fact2. Right or wrong?
&b[0]
decays to a pointerb
. The output coincides with Fact2 .I expected 16 here but I got 8 as the output. I concluded that this is because
a
is afterall a pointer and the output coincides with Fact2 . I got the output similar to Question 2.
a[0]
is pointer. The output coincides with Fact2
a[1]
is pointer. The output coincides with Fact2Please answer the questions and correct me if any of the observations are wrong.
解决方案Please answer the questions and correct me if any of the observations are wrong.
- Is Case 1 output
20
becauseb
was declared as anarray
ofint
egers ieint[]
? The total block in bytes is returned as confirmed by Fact1. Isn't it?
Yes, the result shows the
sizeof(int [5])
. So from Fact1, the size is5*4
- I guess casting
b
toint*
made the difference here. Hereb
is considered a pointer. I confirmed this using Fact2. Right or wrong?
Right. But adding more info:
sizeof
needs only the type of expression and it does not evaluate expression (for value) unless it is VLA type. (From section 6.5.3.4 The sizeof operator of C99 specs)Because you are applying cast on final result, anything after that does not matter.
&b[0]
decays to a pointerb
. The output coincides with Fact2 .
No and yes. Type of
b[0]
isint
and thus type of&b[0]
is alreadyint *
(Recall that[...]
binds tighter than&
). There is no decaying. And yes the output coincides with Fact2.
- I expected 16 here but I got 8 as the output. I concluded that this is because a is afterall a pointer and the output coincides with Fact2 . I got the output similar to Question 2.
a
as pointer to array 2 ofint
. So the printed size is of pointer (to anint
array).
int (*a)[2];
declaresa
as pointer to array 2 ofint
. So you get the size ofpointer to array
.To get the desired result (size of array 2 of pointers to
int
) use:int *a[2];
int (*a)[2]; a anonymous +----+ +----+----+ | a |----->|int |int | +----+ +----+----+ int *b[2]; b +----+----+ |int*|int*| +----+----+ b[0] b[1]
a[0]
is pointer. The output coincides with Fact2a[2]
is pointer. The output coincides with Fact2
As stated earlier,
a
is a pointer to array 2 ofint
. Soa[index]
is an array 2 ifint
. So, type ofa[0]
anda[1]
are array 2 ofint
. So the output is2*4
from Fact 1.
Possibly irrelevant to this answer buta
is uninitialized and using it in expression would cause undefined behaviour. Though it is fine to use insizeof
To understand the output, let's analyse the type of argument of
sizeof
printf("sizeof(b) : %zu\n",sizeof(b)); // int [5] printf("sizeof((int*)b) : %zu\n",sizeof((int*)b)); // int * printf("sizeof(&b[0]) : %zu\n",sizeof(&b[0])); // int * printf("sizeof(a) : %zu\n",sizeof(a)); // int (*) [2] printf("sizeof(a[0]) : %zu\n",sizeof(a[0])); // int [2] printf("sizeof(a[1]) : %zu\n",sizeof(a[1])); // int [2]
A portable program (not foolproof) to confirm the types looks like:
assert(sizeof(b) == sizeof(int [5])); assert(sizeof((int*)b) == sizeof(int *)); assert(sizeof(&b[0]) == sizeof(int *)); assert(sizeof(a) == sizeof(int(*)[2])); assert(sizeof(a[0]) == sizeof(int[2])); assert(sizeof(a[1]) == sizeof(int[2]));
这篇关于使用的sizeof与数组和指针实验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!