Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将字符数组复制到';c';使用指针和不使用字符串库_C - Fatal编程技术网

如何将字符数组复制到';c';使用指针和不使用字符串库

如何将字符数组复制到';c';使用指针和不使用字符串库,c,C,如何使用指针而不使用字符串库将字符数组复制到C中的另一个字符数组中 我试过这个: 头文件: char strconcat(char a[100], char b[100]) { int i=0,j=0; while(a[i] != '\0'){ i++; } while(b[j] != '\0'){ a[i] = b[j]; j++; i++; } return a[100]; }

如何使用指针而不使用字符串库将字符数组复制到
C
中的另一个字符数组中

我试过这个:

头文件:

char strconcat(char a[100], char b[100]) {
    int i=0,j=0;
    while(a[i] != '\0'){
        i++;
    }
    while(b[j] != '\0'){
        a[i] = b[j];
        j++;
        i++;
    }
    return a[100];
}
注意:不能将
字符a[100]
作为函数参数。在C语言中,这个符号会自动转换成字符a[],它相当于字符a


您也不必将事物与0进行比较。默认情况下,省略比较会导致这种情况发生。

第一个版本天真地假设目标中有足够的空间

char *
my_strcat(char *destp, char *srcp)
{
    if(!destp) return(destp); //check that dest is valid
    if(!srcp) return(destp);  //check that src is valid
    char *dp=destp; //save original destp
    while(*dp) { dp++; }      //find end of destp
    while(*srcp)
    {
        *dp++ = *srcp++;      //copy src to dest, then increment
    }
    return(destp);
}
char *
my_strncat(char *destp, char *srcp, long max)
{
    if(!destp) return(destp); //check that dest is valid
    if(!srcp) return(destp);  //check that src is valid
    char *dp=destp; //save original destp
    long x=0;
    while(*dp && (x<max)) { dp++; ++x; }      //find end of destp
    while(*srcp && (x<max))   //copy from src while more space in dst
    {
        *dp++ = *srcp++; ++x; //copy src to dest, then increment
    }
    *dp = '\0'; //ensure destination null terminated
    return(destp);
}
第二个版本允许您指定目标的最大大小

char *
my_strcat(char *destp, char *srcp)
{
    if(!destp) return(destp); //check that dest is valid
    if(!srcp) return(destp);  //check that src is valid
    char *dp=destp; //save original destp
    while(*dp) { dp++; }      //find end of destp
    while(*srcp)
    {
        *dp++ = *srcp++;      //copy src to dest, then increment
    }
    return(destp);
}
char *
my_strncat(char *destp, char *srcp, long max)
{
    if(!destp) return(destp); //check that dest is valid
    if(!srcp) return(destp);  //check that src is valid
    char *dp=destp; //save original destp
    long x=0;
    while(*dp && (x<max)) { dp++; ++x; }      //find end of destp
    while(*srcp && (x<max))   //copy from src while more space in dst
    {
        *dp++ = *srcp++; ++x; //copy src to dest, then increment
    }
    *dp = '\0'; //ensure destination null terminated
    return(destp);
}
char*
my_strncat(char*destp,char*srcp,long max)
{
如果(!destp)返回(destp);//检查dest是否有效
如果(!srcp)返回(destp);//检查src是否有效
char*dp=destp;//保存原始destp
长x=0;

而(*dp&&(X这是可爱的代码…如果在开始复制操作之前,
a
中碰巧已经有100个字符的文本,或者没有空终止符,那么享受一下你的系统会发生什么。可能会重复吗?@MarcB在C中,函数不应该是安全的。它们应该被正确调用。例如double free()将使您崩溃。@MarcB:这不会放置空字节终止符,但它非常接近C的
strcat()
@user3121023
*src
为NUL。赋值表达式的值就是被赋值的值。它不是将
*src
*dst
进行比较。它是将
*dst
的值设置为
*src
,如果恰好是空终止符
\0
,循环将停止。您能解释一下您所做的吗在while语句中,如果可能的话,
dst
src
是指向数组的指针。
dst
指向
dst[0]
,并且
*dst
dst[0]
(获取指针的值)。当我执行
dst++/code>时,我遍历数组,它现在指向
dst[1]
,而
dst
dst[1]
。所以基本上我和你一样,但是我在数组中行走,没有使用
I/j
索引。我可以看到“while(*dst=*src)”会让一些经验较少的人感到困惑。话虽如此,这是一个非常优雅的解决方案。
*dp++=*srcp++;
@Havenard:你是什么意思?我的意思是
*(dp++)=*(srcp++)
(*dp)+=(*srcp)+
将有完全不同的行为。如果没有括号,则可以对其进行解释,除非有什么东西可以保证这些参数的顺序。@Havenard:这是为了实现前一个示例(后一个示例不会编译,
(*dp)+
不是左值),后缀绑定比其他所有绑定都强(包括(前缀)一元)。请参考*vs++的优先级。