you create a string <char c , but copy 10 characters to it. this is mistake.
it is called bufferoverflow: you write in memory that does not belong to you. therefore the behavior is undefined. it may be a failure, it may work fine, or it may change another variable you created.
so the point is to allocate enough memory for c to contain the contents of b:
c = (char *)malloc(sizeof(char) * (sizeof(b)+1));
2 - when copying b to c do not forget to end the line char: '\0' . it is mandatory in standard c. therefore printf("%s",c); knows where to end the line.
3 - you copied 10 characters from b to c , but b contained only 5 characters (a, b, c, d and '\ 0'), so the behavior of memcpy is undefined (for example: memcpy may try to read memory that cannot be read ...).
You can only copy your existing memory: 5 characters b .
4 - I think a good instruction to define b : char b="abcd"; or char b={'a','b','c','d',0};
source share