问道c语言的题

来源:百度知道 编辑:UC知道 时间:2024/07/04 03:33:03
原型是这个
char *strcpy( char *to, const char *from );
请你写一个相同原型,但不会发生越界访问的strcpy函数

char *strcpy(char *to, const char *from);
{
assert((to!=NULL) && (from!=NULL));
char *address = to;
while( (*to++ = * from++) != ‘\0’ )
NULL ;
return address ;
}

char *strncpy( char *to, const char *from ,int n);

C语言防止越界用strncpy代替strcpy,用参数n指出需拷贝的字符串长度

类似的字符串比较函数strncmp代替strcmp

用指针就可以了,判断是否是'\0',是就停止。