Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can an array used in fread/fwrite in a position of a buffer be written with or without & and yield the same result?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 if 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading