Ok, I’m really confused by this behaviour in VS2008.
This code..
char data[512] = "";
char c[1] = "";
c[0] = '1';
strcat(data, c);
.. results in data being set to this string value: 1ÌÌÌÌhÿ
Surely it should just be 1?
How can I ensure data only contains the single char[] that I copy into it (i.e. 1)?
Why does strcat() copy all that garbage? Why does c even contain that garbage?
Thanks for any help
Edit: Thanks all.
The problem here is that you are passing an invalid value to strcat. It expects the second parameter to be a valid c string value. To be valid it must be an array / pointer of
charvalues which ends with a null terminator (\0). The value your are passing does not contain a null terminator and is hence invalid.You need to define a null terminator for the string value
cto be valid. For example