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?
The function fread is declared the following way
size_t fread(void * restrict ptr, size_t size,
size_t nmemb,
FILE * restrict stream);
Its first parameter has the unqualified type void * (actually the parameter type is the qualified type void * restrict) . A pointer to object of any type may be assigned to a pointer of the type void *.
The expression &header has the pointer type uint8_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 of fread is implicitly converted to a pointer of the type uint8_t * and yields the same initial address of the extent of memory occupied by the array.
Thus these calls
fread(&header, sizeof(uint8_t), HEADER_SIZE, input);
fwrite(header, sizeof(uint8_t), HEADER_SIZE, output);
are equivalent in sense that the function fread gets the same values for its parameters.
Consider the following simple demonstration program.
#include <stdio.h>
int main( void )
{
char s[6] = "Hello";
printf( "The value of the expression s is %p\n", ( void * )s );
printf( "The value of the expression &s is %p\n", ( void * )&s );
}
The program output might look like
The value of the expression s is 0x7fffa4577a2a
The value of the expression &s is 0x7fffa4577a2a
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 type char ( * )[6].
However if you will write for example
#include <stdio.h>
int main( void )
{
char s[6] = "Hello";
printf( "%s\n", s );
printf( "%s\n", &s );
}
then the compiler can issue a message for the second call of printf that the function expects an argument of the type char * instead of the type char ( * )[6] though the both values of the expressions are equal each other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With