C 中的奇数字符串问题
我正在编写一些使用函数 gethostbyname() 的代码。此函数要求我向其传递一个我试图查找主机的主机字符串。现在,我在字符数组中声明了字符串,末尾有一个空字节,因此它被视为字符串。
当我执行这样的 printf 时: printf("\n%s\n",hostName);
代码将正确打印并显示如下内容: facebook.com
但是当我尝试打印这样的字符串时: printf("\n%sX\n",hostName);
由于某种原因,输出将是 Xacebook.com
。
有谁知道为什么 X 会覆盖我的字符串的第一个字符?我认为它应该打印成“facebook.comX”。
I am writing some code that uses the function gethostbyname(). This function requires that I pass it a string of the host I am trying to find the host for. Right now I have my string declared in an array of characters, with a null byte at the end so that it is considered a string.
When I do a printf like this:printf("\n%s\n",hostName);
the code will print correctly and say something like: facebook.com
However when I try to print the string like this: printf("\n%sX\n",hostName);
the output will be Xacebook.com
for some reason.
Does anyone know why the X would overwrite the first character of my string? I would think that it should print like "facebook.comX".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字符串末尾有一个 \r。这会将光标移回行首。我猜您正在从文件中读取主机名?
You have a \r at the end of the string. That moves the cursor back to the start of the line. I'm guessing that you are reading in the hostname from a file?