简体   繁体   中英

unexpected behavior while appending chars using strcat

char buf[256];
char c48 = (char) 48;
char c49 = (char) 49;
char c50 = (char) 50;
char c51 = (char) 51;
char c52 = (char) 52;
char c53 = (char) 53;
strcpy(buf, &c48);
strcat(buf, &c49);
strcat(buf, &c50);
strcat(buf, &c51);
strcat(buf, &c52);
strcat(buf, &c53);
puts(buf);

I expected to output string to be 012345 However when i did a cout this is the result 0?10?210?3210?43210?543210?. I don't understand why this happens. Could it be that the buf gets copied using the strcat method? Sorry for this basic question. I come fron java background.

strcpy(buf, &c48);

The second parameter is not a pointer to a NUL-terminated string. This line, and all lines after it, exhibit undefined behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM