Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_String_Function - Fatal编程技术网

尝试在C中创建一个函数,将一个字符串复制到另一个字符串上

尝试在C中创建一个函数,将一个字符串复制到另一个字符串上,c,string,function,C,String,Function,我必须创建一个函数,它接受两个字符串指针作为参数——一个有内容,一个为空,并将第一个指针的内容复制到第二个指针上 我该怎么做呢?下面是我的代码,但它在运行时会崩溃: #include <stdio.h> void strcopy1(char* mainStr[], char* emptyStr[], int size) { for(int i = 0; i < size; i++) { *emptyStr[i] = *mainStr[i]; //

我必须创建一个函数,它接受两个字符串指针作为参数——一个有内容,一个为空,并将第一个指针的内容复制到第二个指针上

我该怎么做呢?下面是我的代码,但它在运行时会崩溃:

#include <stdio.h>

void strcopy1(char* mainStr[], char* emptyStr[], int size)
{
   for(int i = 0; i < size; i++)
   {
       *emptyStr[i] = *mainStr[i];   //The issue is on this line. How do I do this?
   }
}

int main(void)
{
    char *s1 = "Barrel";
    char *e1;
    printf("mainStr is: %s\n", s1);
    printf("emptyStr is: %s\n", e1);
    strcopy1(&s1, &e1, 7);
    printf("mainStr is: %s\n", s1);
    printf("emptyStr is: %s\n", e1);
}
#包括
void strcopy1(字符*mainStr[],字符*emptyStr[],整数大小)
{
对于(int i=0;i

提前感谢。

我认为问题在于调用函数的线路。我为你找到了一个很好的解决方案

copy_string(char *target, char *source)
{
    while(*source)
    {
        *target = *source;        
        source++;        
        target++;
    }    
    *target = '\0';
}

此函数将为您提供帮助。

我认为问题在于您调用函数的线路。我为你找到了一个很好的解决方案

copy_string(char *target, char *source)
{
    while(*source)
    {
        *target = *source;        
        source++;        
        target++;
    }    
    *target = '\0';
}
此函数将为您提供帮助。

几个问题:

  • 您没有为目标字符串分配空间
  • 您不需要向变量传递指针,只需传递指针变量本身
  • 在调用函数之前不能打印空字符串,因为尚未初始化其任何内容
  • 数组的元素是
    char
    ,而不是指向
    char
    ,因此不需要使用
    *mainStr[i]
    *emptyStr[i]
  • 代码:

    #包括
    void strcopy1(字符主字符串[],字符空字符串[],整数大小)
    {
    对于(int i=0;i
    几个问题:

  • 您没有为目标字符串分配空间
  • 您不需要向变量传递指针,只需传递指针变量本身
  • 在调用函数之前不能打印空字符串,因为尚未初始化其任何内容
  • 数组的元素是
    char
    ,而不是指向
    char
    ,因此不需要使用
    *mainStr[i]
    *emptyStr[i]
  • 代码:

    #包括
    void strcopy1(字符主字符串[],字符空字符串[],整数大小)
    {
    对于(int i=0;i
    以下是您的代码,其中带有行内注释的更正:

    // pointer to pointer is not needed, change argument to char *
    //void strcopy1(char* mainStr[], char* emptyStr[], int size)
    void strcopy1(char* mainStr, char* emptyStr, int size)
    {
       for(int i = 0; i < size; i++)
       {
           //*emptyStr[i] = *mainStr[i];   //The issue is on this line. How do I do this?
           emptyStr[i] = mainStr[i]; //reference variable, not pointer to variable
       }
    }
    
    int main(void)
    {
        char *s1 = "Barrel";
        char *e1 = {0};  // initialize variable
        e1 = malloc(strlen(s1)+1); //allocate memory to target string (+1 for NULL)
        if(e1)//ensure e1 memory was created
        {
           printf("mainStr is: %s\n", s1);
           printf("emptyStr is: %s\n", e1);
           strcopy1(s1, e1, 7); //string variables are already pointers
                             //no need for the "address of" operator
           printf("mainStr is: %s\n", s1);
           printf("emptyStr is: %s\n", e1);
           free(e1); //always free memory when explicitly created using malloc()
        }   
    
        return 0;//add return to match your 'main' prototype
    }
    
    //不需要指向指针的指针,请将参数更改为char*
    //void strcopy1(字符*mainStr[],字符*emptyStr[],整数大小)
    void strcopy1(char*mainStr、char*emptyStr、int size)
    {
    对于(int i=0;i
    在评论中回答您的问题:
    在将e1行更改为“char*e1[7];”或“char e1=”“;并删除符号和之后,它仍然崩溃

    1) 将
    char*e1
    更改为
    char*e1[7]
    正在创建
    char*[7]
    ,但作业需要
    char*
    char[7]
    。两者都将创建足够用作作为参数传递的C字符串的容器。(但是
    char*
    需要分配内存(
    [m][C]alloc
    )(使用前)


    2) 关于删除符号,上面也解释了这一点,但基本上,
    &
    是运算符的地址,因为C字符串名称(变量)已经指向字符串的地址,所以不需要使用
    &
    运算符。

    以下是带注释的代码:

    // pointer to pointer is not needed, change argument to char *
    //void strcopy1(char* mainStr[], char* emptyStr[], int size)
    void strcopy1(char* mainStr, char* emptyStr, int size)
    {
       for(int i = 0; i < size; i++)
       {
           //*emptyStr[i] = *mainStr[i];   //The issue is on this line. How do I do this?
           emptyStr[i] = mainStr[i]; //reference variable, not pointer to variable
       }
    }
    
    int main(void)
    {
        char *s1 = "Barrel";
        char *e1 = {0};  // initialize variable
        e1 = malloc(strlen(s1)+1); //allocate memory to target string (+1 for NULL)
        if(e1)//ensure e1 memory was created
        {
           printf("mainStr is: %s\n", s1);
           printf("emptyStr is: %s\n", e1);
           strcopy1(s1, e1, 7); //string variables are already pointers
                             //no need for the "address of" operator
           printf("mainStr is: %s\n", s1);
           printf("emptyStr is: %s\n", e1);
           free(e1); //always free memory when explicitly created using malloc()
        }   
    
        return 0;//add return to match your 'main' prototype
    }
    
    //不需要指向指针的指针,请将参数更改为char*
    //void strcopy1(字符*mainStr[],字符*emptyStr[],整数大小)
    void strcopy1(char*mainStr、char*emptyStr、int size)
    {
    对于(int i=0;i