I am trying to copy the memory value of int into the char buffer. The code looks like below,
#define CPYINT(a, b) memcpy(a, &b, 4)
............
char str1[4];
int i = 1;
CPYINT(str1, i);
printf("%s",s);
...........
When I print str1 it’s blank. Please clarify.
You are copying the byte representation of an integer into a char array. You then ask
printfto interpret this array as a null terminating string :str1[0]being zero, you are essentially printing an empty string (I’m skipping the endianness talk here).What did you expect ? Obviously, if you wanted to print a textual representation of the integer
i, you should useprintf("%d", i).