String functions expect ASCII zero (null-termination) at the end strings. var1 is too short by one byte and is not null-terminated.
Code:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char var1[5] = {'\0'};
int var2 = 0;
strcpy(var1,"FP10");
printf("Value of var1 is %s\n", var1);
printf("Vaue of var2 is %d\n", var2);
memcpy(&var2, &var1, sizeof(var2));
printf("Vaue of var2 is %d\n", var2);
printf("String value of var2 is %s\n", &
return 0;
}
You can do the same thing with a union.
Code:
union{
char var1[5];
int var2;
} u;
strcpy(u.var1,"FP10");
printf("Value of u.var1 is %s\n", u.var1);
printf("Vaue of u.var2 is %d\n", u.var2);
return 0;