【问题标题】:Function to copy char pointer content crashes when printing second index of copy打印副本的第二个索引时复制字符指针内容的函数崩溃
【发布时间】:2013-10-08 03:00:55
【问题描述】:

我在将 char 指针指向的内容复制到另一个时遇到问题,即使我在使用 strcpy 之前已为其分配内存。我已经看到了一些关于 strdup 的建议,但我想知道在不需要它的情况下该怎么做。这是我的主要代码

int main (void)
{
    char word[20];
    leaftype destiny;
    while(1)
    {
        printf("\nType in a word: ");
        scanf("%s",word);
        copy_leaf(&destiny,palavra);
        if (palavra[0] == '0') break;
    }

    system("pause");

    return 0;
}

我遇到的问题是函数copy_leaf:

void copy_leaf(leaftype* destiny, leaftype source)
{
    printf("\n====start of copy_leaf======\n");
    int i;
    printf("strlen of source: %d",strlen(source));
    printf("\nsource: ");
    for(i = 0; i <= strlen(source); i++)
    {
        if(i == strlen(source))
        {
            printf("\\0");
        }
        else printf("%c-",source[i]);
    }
    *destiny = malloc((strlen(source)+1)*sizeof(char));
    strcpy(*destiny,source);
    printf("\nstrlen of destiny: %d",strlen(*destiny));
    printf("\ndestiny: ");
    for(i = 0; i <= strlen(*destiny); i++)
    {
        if(i == strlen(*destiny))
        {
        printf("\\0");
        }
        else printf("%c-",*destiny[i]);
    }
    printf("\n===end of copy_leaf======\n");
}

leaftype 定义为:

typedef char* leaftype;

当我运行以“example”作为输入的代码时,我进入控制台:

Type in a word: 
====start of copy_leaf======
strlen of source: 7
source: e-x-a-m-p-l-e-\0
strlen of destiny: 7
destiny: e-

它崩溃了(“program.exe 已停止工作等”在 Windows 7 上)。我正在使用 devcpp,但我的文件以 C 扩展名命名。谁能帮我修复这个 char* 到 char* 内容副本?我需要一个函数来做到这一点,因为我需要在我的 C 文件中多次将一个字符串的内容复制到另一个字符串。提前致谢!

ps:我已经在 copy_leaf 功能中尝试过的东西(绝望的解决方案):

  • leaftype source 更改为 const leaftype source(这将是一个 const char* source
  • 制作*destiny = strcpy(*destiny,source),因为strcpy返回一个指向目标字符串的指针

【问题讨论】:

    标签: c string pointers malloc


    【解决方案1】:

    你不应该使用*destiny[i],但你需要像这行一样使用(*destiny)[i]

        else printf("%c-",(*destiny)[i]);
    

    顺便说一句,命运是一个双指针,我不认为你真的需要一个双指针。

    【讨论】:

    • 如果您以新手可以理解的方式解释其中的区别,我会 +1 ;)
    • here所述,需要双指针
    【解决方案2】:
    printf("%c-",*destiny[i]);
    

    destiny 是一个字符**,[] 优先于 *。

    因此这被解释为:

    printf("%c-",*(destiny[i]));
    

    当你真正想要的时候:

    printf("%c-", (*destiny)[i]);
    

    即当您实际上想要第一个(也是唯一一个)指针的第 i 个元素时,您正在读取第 i 个指针的第一个元素。

    【讨论】:

      【解决方案3】:

      为什么我喜欢这样? 以下是需要进行的更正。

       void copy_leaf(leaftype* destiny, leaftype source)
      

      改成

      void copy_leaf(leaftype destiny, leaftype source)
      
      destiny = malloc((strlen(source)+1)*sizeof(char));
      
      strcpy(destiny,source);
      
      
      for(i = 0; i < strlen(destiny); i++)
          {
              printf("%c-",destiny[i]);
      
      
          }
      

      顺便说一句,strcpy 的正确原型应该是源数据应该总是- const char *。

      【讨论】:

      • 这会改变行为。在 OP 的版本中,命运在调用者中被修改。您现在只修改本地副本。
      • 另外,strcpy 的第二个参数是 const char*,但这意味着 strcpy 不会修改数据。调用者不需要使用 const char*; char* 是兼容的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多