From the C Standard (7.23.6.1 The memset function)
2 The memset function copies the value of c (converted to an unsigned
char) into each of the first n characters of the object pointed to by
s.
So this call
memset(&a, 5, sizeof(int));
does not set the variable a
equal to 5
. Internally the variable will look like
0x05050505
Here is a demonstrative program
#include <stdio.h>
#include <string.h>
int main(void)
{
int a;
memset( &a, 5, sizeof( int ) );
printf( "%#x\n", ( unsigned )a );
return 0;
}
Its output is
0x5050505
You should use the function memset
with integers with caution because in general it can produce a trap value. Also the result depends on how internally integers are stored starting from MSB or LSB.
P.S. You declared a variable inside a block scope with no linkage. It is also a variable definition that has automatic storage duration. As the variable explicitly was not initialized then it has an indeterminate value. You may apply the address of operator &
to get the address of the memory extent where the variable is defined.