内存设置+空白 +内存复制
我如何将一个大小为 100 的字符数组设置为空白,然后将 10 个字符从其他字符复制到同一字符串。 例如:
有一个char数组a[100] 要做的事:将其全部设置为空白
现在有另一个数组:b[10](假设它填充了一些长度为 9 的字符串) 要做的事:将此数组复制到前一个数组
What iam doing is : memset(a, ' ', sizeof(a));
350 memcpy(a,b, strlen(b))
,但是我在复制 10 个字符后没有获得我设置的空间。
How can i set a character array say of size 100 to whitespace and then copy 10 charters to that same string from other.
For example:
there is one char array a[100]
To do : set all of it to whitespace
Now there is another array : b[10] (suppose this is filled with some string of length 9)
To do : copy this array to the previous one
What iam doing is : memset(a, ' ', sizeof(a));
350 memcpy(a,b, strlen(b))
But iam not getting space that i had set after 10 chars has been copied .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试以下操作:
运行此命令:
Try the following:
Running this:
您在数组的最后一个位置缺少空
\0
字符。You are missing the null
\0
character at the last position of the array.你会看到的。尝试以下代码:
原因是当您memcpy
字符串到a
时,一个NULL字符被放置在a[10]
中。如果只输出a
,输出将在NULL之前停止。You'll see. Try the following code:
The reason is when youmemcpy
the string toa
, a NULL character is placed ina[10]
. If you only outputa
, the output will stop just before the NULL.您不认为您需要将类型转换为
(char *)
don't you think that you need to type cast to
(char *)