我如何才能获得s[7]的s?
我没有发现strncpy和之间的任何区别memcpy.如果我想打印输出s,以及s[7](像qwertyA),我必须在以下代码中进行哪些更改:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char s[10] = "qwerty", str[10], str1[10];
s[7] = 'A';
printf("%s\n",s);
strncpy(str,s,8);
printf("%s\n",str);
memcpy(str1,s,8);
printf("%s\n",str1);
return 0;
}
/*
O/P
qwerty
qwerty
qwerty
*/
Run Code Online (Sandbox Code Playgroud) 只是好奇地知道(因为我们经常使用这些功能).我没有看到strncpy()和memcpy()之间有任何实际区别.有效地说,不值得
char* strncpy (char *dst, const char *src, size_t size)
{
return (char*)memcpy(dst, src, size);
}
Run Code Online (Sandbox Code Playgroud)
或者我错过任何副作用?有一个类似的早期问题,但找不到确切的答案.