69,369
社区成员
发帖
与我相关
我的任务
分享
/* Test function strncpy() */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char destination[] = "This string will be replaced";
char source[] = "This string will be copied in part";
size_t n1 = 26; /* Number of characters to be copied */
strncpy(destination , source , n1);
printf("\n%s\n" , destination);
char* str = "MyString";
char* pStr = (char*)malloc(7 * sizeof(char));
size_t n2 = 6; /* Number of characters to be copied */
strncpy(pStr , str , n2);
printf("\n%s\n" , pStr);
return 0;
}
char destination[] = "This string will been replaced";
char source[] = "This string will be copied in part";
size_t n1 = 26; /* Number of characters to be copied */
strncpy(destination , source , n1);
printf("\n%s\n" , destination);
char destination[] = "This string will be replaced";//这个的长度是28
strncpy(destination , source , n1);//运行这个只是将source中的前26个字符将
//destination的前26个字符覆盖了,但是destination后边还有两个字符ed;所以输出的时候是
//This string will be copied(ed),而不是This string will be copied