【问题标题】:Trying to make a function in C that copies one string onto another试图在 C 中创建一个将一个字符串复制到另一个字符串的函数
【发布时间】:2017-04-14 18:54:01
【问题描述】:

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

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

#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);
}

提前致谢。

【问题讨论】:

  • 你没有为e1分配任何内存来指向。
  • 你不应该传递&amp;s1&amp;e1,只传递s1e1
  • 1) *emptyStr[i] = *mainStr[i]; --> emptyStr[i] = mainStr[i]; 2) 删除 printf("emptyStr is: %s\n", e1); (e1 是未初始化的变量,不是空的。) 3) strcopy1(&amp;s1, &amp;e1, 7); --> strcopy1(&amp;s1, &amp;e1, 1);跨度>
  • 将 e1 行更改为 "char *e1[7];" 后或 "char e1 = " "; 并删除 & 符号,它仍然崩溃。
  • 您需要注意编译器警告。由于你的错误,你应该得到一堆警告。

标签: c string function


【解决方案1】:

几个问题:

  1. 您没有为目标字符串分配空间。
  2. 您不需要将指针传递给变量,只需传递指针变量本身即可。
  3. 在调用函数之前不能打印空字符串,因为你还没有初始化它的任何内容。
  4. 数组的元素是char,而不是指向char的指针,所以你不需要使用*mainStr[i]*emptyStr[i]

代码:

#include <stdio.h>

void strcopy1(char mainStr[], char emptyStr[], int size)
{
   for(int i = 0; i < size; i++)
   {
       emptyStr[i] = mainStr[i];
   }
}

int main(void)
{
    char *s1 = "Barrel";
    size_t len = strlen(s1) + 1; // Add 1 for the trailing null byte
    char *e1 = malloc(len);
    printf("mainStr is: %s\n", s1);
    strcopy1(s1, e1, len);
    printf("mainStr is: %s\n", s1);
    printf("emptyStr is: %s\n", e1);
    free(e1); // Always free dynamically-allocated memory
}

【讨论】:

    【解决方案2】:

    我认为问题出在你调用函数的那一行。我找到了适合您的解决方案。

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

    这个函数会帮你搞定的。

    【讨论】:

      【解决方案3】:

      首先,您尝试显示一个未初始化的字符串,这是不可能的。 其次,我不明白为什么参数是指针数组。 这是我的建议:

      void strcopy1(char *mainStr, char *emptyStr) {
         while (*mainStr) {
            *emptyStr = *mainStr;
             mainStr++;
             emptyStr++;
          }
        *emptyStr = '\0';
       }
      
      int main(void)
      {
         char *s1 = "Barrel";
         char *e1;
         printf("mainStr is: %s\n", s1);
         strcopy1(&s1, &e1);
         printf("mainStr is: %s\n", s1);
         printf("emptyStr is: %s\n", e1);
      }
      

      【讨论】:

        【解决方案4】:

        以下是您的代码,其中包含在线注释的更正:

        // 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
        }
        

        在 cmets 中回答您的问题: 将 e1 行更改为 "char *e1[7];" 后或 "char e1 = " "; 并删除 & 符号,它仍然崩溃。

        1) 将char *e1 更改为char *e1[7] 将创建char *[7],但您需要char *char [7] 来完成这项工作。两者都将创建足以用作作为参数传递的 C 字符串的容器。 (但是char *在使用前需要分配内存([m][c]alloc

        2) 关于删除 & 符号,上面也解释过,但基本上,&amp; 是运算符的地址,因为 C 字符串 names (变量)已经指向字符串,不需要&amp; 运算符。

        【讨论】:

          猜你喜欢
          • 2022-11-30
          • 2021-08-28
          • 2012-05-09
          • 1970-01-01
          • 1970-01-01
          • 2010-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多