[英]printf strange behavior when printing char - LLDB debugger
While working with a static C++ library for my iOS Xcode project, I met a strange behavior with printf and Xcode LLDB debugger, sometimes my print results of printf("%s", char*)
was the char concatenated with numbers or garbage characters. 当为我的iOS Xcode项目使用静态C ++库时,我遇到了printf和Xcode LLDB调试器的奇怪行为,有时我的
printf("%s", char*)
打印结果是用数字或垃圾字符连接的char。 The attached image speaks for itself. 所附图片说明了一切。 Any idea?
任何想法?
Seems to work as expected. 似乎按预期工作。
%s
requires NUL-terminated string, that is, for a given char*
, print anything until the first '\\0'
. %s
需要以NUL终止的字符串,也就是说,对于给定的char*
,请打印任何内容,直到第一个'\\0'
为止。 You have to allocate 5 bytes and assign a[4]=0;
您必须分配5个字节并分配
a[4]=0;
if you want your array to be suitable for %s
. 如果您希望数组适合
%s
。
The character array a
needs to be terminated with a '\\0'
if you're printing it with a %s
format specifier: 如果要使用
%s
格式说明符来打印字符数组a
,则必须以'\\0'
结尾:
char * a = new char[5];
a[0] = 'i';
a[1] = 'p';
a[2] = 'o';
a[3] = 'd';
a[4] = '\0';
printf("word: %s\n", a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.