Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.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_Pointers_String Concatenation - Fatal编程技术网

C 我的连接操作有什么问题?

C 我的连接操作有什么问题?,c,pointers,string-concatenation,C,Pointers,String Concatenation,我试图实现strcat(),所以我想出了这段代码。但是,我不知道这有什么问题?这是我的错 我在想这可能是内存分配混乱?是这样吗?如果不使用malloc(),如何修复它 #包括 char*strcat(char*s,char*d){ 而(*s++!='\0'); 而(*s++=*d++); *s='\0'; 返回s; } int main(){ 字符s[20]=“源”; 字符d[20]=“dest”; printf(“%s\n”,strcat(s,d)); 返回0; } 我必须在s之前确定d 字

我试图实现strcat(),所以我想出了这段代码。但是,我不知道这有什么问题?这是我的错

我在想这可能是内存分配混乱?是这样吗?如果不使用
malloc()
,如何修复它

#包括
char*strcat(char*s,char*d){
而(*s++!='\0');
而(*s++=*d++);
*s='\0';
返回s;
}
int main(){
字符s[20]=“源”;
字符d[20]=“dest”;
printf(“%s\n”,strcat(s,d));
返回0;
}
我必须在
s
之前确定
d

  • 字符串位于只读内存中
  • 字符串
    s
    不够长
  • 要修复:

       ...
       #define S_STR "source"
    
       char *d= "dest";
       char *s= S_STR;
       s = malloc(strlen(s) + strlen(d) + 1);
       strcpy(s, S_STR);
       printf("%s\n",strcat(s,d));
       free(s);
       return 0;
    }
    
  • 字符串位于只读内存中
  • 字符串
    s
    不够长
  • 要修复:

       ...
       #define S_STR "source"
    
       char *d= "dest";
       char *s= S_STR;
       s = malloc(strlen(s) + strlen(d) + 1);
       strcpy(s, S_STR);
       printf("%s\n",strcat(s,d));
       free(s);
       return 0;
    }
    

    s,d是字符串常量!你不应该做这种事。
    有一个像char s[100]这样的大数组,将源代码复制到它,然后使用连接。记住,s应该有空间容纳d的内容

    s,d是字符串常量!你不应该做这种事。
    有一个像char s[100]这样的大数组,将源代码复制到它,然后使用连接。记住,s应该有空间容纳d的内容

    您声明的字符串s和d是常量字符串文字,无法修改它们。 您应该声明两个字符数组,并确保要复制到的那个字符数组足够大,可以容纳另一个字符数组

    #include <stdio.h>
    
    char *strcat(char *s,char *d)
    {
        //Get length of s string
        size_t len = strlen(s);
    
        //declare new pointer and assign it the value of s
        char *ptr = s;
    
        //move the pointer to the end of the string
        ptr += len;
    
        //copy contentes of d string to s string
        while( *d != '\0' )
        {
            *ptr++ = *d++;
        }
    
        *ptr = '\0';
    
        //return s
        return s;
    }
    
    int main()
    {
        //make sure s array length is big enough to accomodate d string
        char s[50] = "source";
    
        char d[] = "dest";
    
        printf("%s\n",strcat(s,d));
    
        return 0;
    }
    
    #包括
    char*strcat(char*s,char*d)
    {
    //获取s字符串的长度
    尺寸长度=标准长度;
    //声明新指针并将其赋值为s
    char*ptr=s;
    //将指针移到字符串的末尾
    ptr+=len;
    //将d字符串的内容复制到s字符串
    而(*d!='\0')
    {
    *ptr++=*d++;
    }
    *ptr='\0';
    //返回s
    返回s;
    }
    int main()
    {
    //确保s数组长度足够大以容纳d字符串
    字符s[50]=“源”;
    字符d[]=“dest”;
    printf(“%s\n”,strcat(s,d));
    返回0;
    }
    
    您声明的字符串s和d是常量字符串文字,无法修改它们。 您应该声明两个字符数组,并确保要复制到的那个字符数组足够大,可以容纳另一个字符数组

    #include <stdio.h>
    
    char *strcat(char *s,char *d)
    {
        //Get length of s string
        size_t len = strlen(s);
    
        //declare new pointer and assign it the value of s
        char *ptr = s;
    
        //move the pointer to the end of the string
        ptr += len;
    
        //copy contentes of d string to s string
        while( *d != '\0' )
        {
            *ptr++ = *d++;
        }
    
        *ptr = '\0';
    
        //return s
        return s;
    }
    
    int main()
    {
        //make sure s array length is big enough to accomodate d string
        char s[50] = "source";
    
        char d[] = "dest";
    
        printf("%s\n",strcat(s,d));
    
        return 0;
    }
    
    #包括
    char*strcat(char*s,char*d)
    {
    //获取s字符串的长度
    尺寸长度=标准长度;
    //声明新指针并将其赋值为s
    char*ptr=s;
    //将指针移到字符串的末尾
    ptr+=len;
    //将d字符串的内容复制到s字符串
    而(*d!='\0')
    {
    *ptr++=*d++;
    }
    *ptr='\0';
    //返回s
    返回s;
    }
    int main()
    {
    //确保s数组长度足够大以容纳d字符串
    字符s[50]=“源”;
    字符d[]=“dest”;
    printf(“%s\n”,strcat(s,d));
    返回0;
    }
    
    我把它修好了

    #include <stdio.h>
    #define SIZE 20
    
    char *strocat(char *s,char *d){
    
     char *temp = s;
    
     while(*s++ != '\0');
     *--s;
     while(*s++ = *d++);
    
     *s = '\0';
    
     return temp;
    }
    
    int main(){
    
    char s[SIZE] = "source";
    char d[SIZE] = "dest";
    
    printf("%s\n",strocat(s,d));
    
    return 0;
    }
    
    #包括
    #定义尺寸20
    char*strocat(char*s,char*d){
    char*temp=s;
    而(*s++!='\0');
    *--s;
    而(*s++=*d++);
    *s='\0';
    返回温度;
    }
    int main(){
    字符s[SIZE]=“源”;
    字符d[SIZE]=“dest”;
    printf(“%s\n”,strocat(s,d));
    返回0;
    }
    
    我把它修好了

    #include <stdio.h>
    #define SIZE 20
    
    char *strocat(char *s,char *d){
    
     char *temp = s;
    
     while(*s++ != '\0');
     *--s;
     while(*s++ = *d++);
    
     *s = '\0';
    
     return temp;
    }
    
    int main(){
    
    char s[SIZE] = "source";
    char d[SIZE] = "dest";
    
    printf("%s\n",strocat(s,d));
    
    return 0;
    }
    
    #包括
    #定义尺寸20
    char*strocat(char*s,char*d){
    char*temp=s;
    而(*s++!='\0');
    *--s;
    而(*s++=*d++);
    *s='\0';
    返回温度;
    }
    int main(){
    字符s[SIZE]=“源”;
    字符d[SIZE]=“dest”;
    printf(“%s\n”,strocat(s,d));
    返回0;
    }
    
    所以我必须使用malloc?不,定义
    s
    就足够了,这样它将包含两个字符串和空终止符:
    char s[11]=“source”
    ,例如。您还混淆了源和目标:您将目标添加到源。您还返回了
    s
    ,它指向最后的终结者,所以打印永远不会成功。另外,函数名无效,这是一个保留名称。@取消函数名可以,因为除了库函数外,还有一个不同的返回类型look close,是的,我注意到sso我必须使用malloc?否,定义
    s
    就足够了,这样它将包含两个字符串和空终止符:
    char s[11]=“source”
    ,例如。您还混淆了源和目标:您将目标添加到源。您还返回了
    s
    ,它指向最终终止符,因此打印将永远不会成功。另外,函数名无效,这是一个保留名称。@解卷函数名没有问题,因为除了库函数外,还有一个不同的返回类型look close,是的,我注意到如果我传递一个字符数组会怎么样?是的,只是注意到我自己如果传递一个字符数组会怎么样?是的,只是注意到我自己这是代码,没有答案。可以通过解决操作规程中的问题来改进,这是代码,而不是答案。可以通过解决OP中的问题来改进