开发者

Odd String Issue in C

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 开发者_JAVA技巧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".


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜