代码: 全选
#include "stdio.h"
#include "string.h"
#define SIZE 10
char * my_strncpy(char * target, char * source, int n);
int main(int argc, char const *argv[])
{
char * target;
char source[] = "hello, world";
target = my_strncpy(target, source, SIZE);
puts(target);
return 0;
}
char * my_strncpy(char * target, char * source, int n)
{
int index;
for (index = 0; index < n; index++)
{
if (index < strlen(source))
target[index] = source[index];
else
break;
}
target[index] = '\0';
return target;
}
代码: 全选
段错误 (核心已转储)
大家给指点一下。谢谢。