|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
strncpy函数将指定字符串的指定长度复制到另外一个字符串,我想在前面加一个偏移量,从指定的位置开始复制,而不是从头开始,将指定位置的指定长度的指定字符串复制到另外一个字符串,可是最后的输出一直的空
直接拿strncpy的源码进行修改的,求大佬帮忙解惑
char* strncpy_ch(char* dest, const char* source, size_t count,size_t begin)
{
char* new_dest = dest+begin;
char* start=new_dest;
while (count && (*new_dest++ = *source++))
count--;
if(count)
while (--count)
*new_dest++='\0';
return(start);
}
本帖最后由 jackz007 于 2022-4-24 18:03 编辑
偷懒写的
- #include <stdio.h>
- #include <string.h>
- char * strncpy_ch(char * dest , const char * source , size_t count , size_t begin)
- {
- return strncpy(dest , & source[begin] , count) ;
- }
复制代码
自己写的
- #include <stdio.h>
- char * strncpy_ch(char * dest , const char * source , size_t count , size_t begin)
- {
- int c , i , n ;
- for(c = n = 0 ; source[n] ; n ++) ;
- if(n > 0 && count < n) for(c = 0 , i = begin ; c < count && source[i] ;) dest[c ++] = source[i ++] ;
- dest[c] = '\0' ;
- return dest ;
- }
复制代码
|
|