为什么可以在带有或不使用&amp的情况下写下缓冲区位置中的径向/fwrite中使用的数组;并产生相同的结果?
为什么这些产生相同的结果?
uint8_t header[HEADER_SIZE];
fread(&header, sizeof(uint8_t), HEADER_SIZE, input);
fwrite(&header, sizeof(uint8_t), HEADER_SIZE, output);
uint8_t header[HEADER_SIZE];
fread(header, sizeof(uint8_t), HEADER_SIZE, input);
fwrite(header, sizeof(uint8_t), HEADER_SIZE, output);
在第一个版本中,&
是否不仅可以访问数组的位置,而不是像第二版中的数组本身一样?我是否知道为什么会使用&
,但我认为数组本身就是“指针”,因此一般来说,没有理由再获得地址?在这种情况下只是冗余吗?
Why do these yield the same result?
uint8_t header[HEADER_SIZE];
fread(&header, sizeof(uint8_t), HEADER_SIZE, input);
fwrite(&header, sizeof(uint8_t), HEADER_SIZE, output);
uint8_t header[HEADER_SIZE];
fread(header, sizeof(uint8_t), HEADER_SIZE, input);
fwrite(header, sizeof(uint8_t), HEADER_SIZE, output);
In the first version, would the &
not give access just to the location of the array, and not the array itself like in the second version? Were it a single valuable I understand why an &
would be used, but I assumed an array is itself a "pointer", generally speaking, so there would be no reason to get its address again? Is it just redundancy in that case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
函数
fread
声明以下方式其第一个参数具有不合格的类型
void *
(实际上,参数类型是合格类型void *限制 *限制
)。指向任何类型对象的指针可以分配给类型void *
的指针。表达式
& header
具有指针类型uint8_t( *)[header_size]
,并产生数组占据的内存程度的初始地址,表达式
标题
用作fread
的呼叫的参数,将其隐式转换为类型uint8_t *
的指针被阵列占据。因此,这些调用
是等效的,因为函数
fread
获得其参数的值相同。考虑以下简单的演示程序。
程序输出可能看起来像是
您可以看到的两个输出值彼此平等,尽管在第一个调用中,使用的参数表达式具有类型
char *
,而在第二种情况下,使用的参数表达式具有类型char( *)[6]
。但是,如果您要编写,
则编译器可以为
printf
的第二个呼叫发出消息,该函数期望该函数的参数char *
而不是类型<代码> char( *)[6] 虽然表达式的两个值彼此平等。The function
fread
is declared the following wayIts first parameter has the unqualified type
void *
(actually the parameter type is the qualified typevoid * restrict
) . A pointer to object of any type may be assigned to a pointer of the typevoid *
.The expression
&header
has the pointer typeuint8_t ( * )[HEADER_SIZE]
and yields the initial address of the extent of memory occupied by the array,The expression
header
used as an argument of a call offread
is implicitly converted to a pointer of the typeuint8_t *
and yields the same initial address of the extent of memory occupied by the array.Thus these calls
are equivalent in sense that the function
fread
gets the same values for its parameters.Consider the following simple demonstration program.
The program output might look like
As you can see the both outputted values are equal each other though in the first call the used argument expression has the type
char *
while in the second case the used argument expression has the typechar ( * )[6]
.However if you will write for example
then the compiler can issue a message for the second call of
printf
that the function expects an argument of the typechar *
instead of the typechar ( * )[6]
though the both values of the expressions are equal each other.标题
是char
的数组。将数组作为参数有效地传递给其第一个元素的指针,fread
和fwrite
接受指示void
作为其第一个参数,所以fread(标题,sizeof(uint8_t),header_size,input);
很好,尽管为了与数组定义保持一致,但编写:&amp; header
具有不同的类型:指向一个或多个数组的header_size
字符(uint8_t(*)[header_size]
)。由于fread
接受任何形式的指针,因此编译器将接受呼叫fread(&amp; header,sizeof(uint8_t),header_size,header_size,input);
而无需警告。因为
header
和&amp; header
具有相同的地址,fread
将按预期行事,尽管该行为不能由C标准保证。但是请注意,如果header
是指向char
的指针,则传递&amp; header
确实会产生非常不同的结果,但确实不确定的行为确实是指针将被覆盖,并且可能有更多字节以外...header
is an array ofchar
. Passing an array as an argument effectively passes a pointer to its first element,fread
andfwrite
accept pointers tovoid
as their first argument sofread(header, sizeof(uint8_t), HEADER_SIZE, input);
is fine although for consistency with the array definition it would be safer to write:&header
has a different type: pointer to one or more arrays ofHEADER_SIZE
characters (uint8_t (*)[HEADER_SIZE]
). Sincefread
accepts any kind of pointer, the compiler will accept the callfread(&header, sizeof(uint8_t), HEADER_SIZE, input);
without a warning.Because
header
and&header
have the same address,fread
will behave as expected, albeit this behavior is not guaranteed by the C Standard. Note however that ifheader
was a pointer tochar
, passing&header
would produce a very different result, undefined behavior indeed as the value of the pointer would be overwritten and probably many more bytes beyond...