【问题标题】:Copying string from one structure to another structure将字符串从一个结构复制到另一个结构
【发布时间】:2015-04-19 01:57:18
【问题描述】:

我正在尝试编写一个按字母顺序对小字典进行排序的程序。为此,我需要能够将字符串从未排序的字典复制到已排序的字典。如果我尝试这样复制整个字符串:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>

struct entry
{
    char word[15];
    char definition[50];
};


void dictionarySort(struct entry dictionary[]) {
    int i;

    struct entry dictionary2[100] = {{}};

    for (i = 0; i <= strlen(&dictionary->word[0]); i++) {
        dictionary2[0].word[i] = dictionary[0].word[i];

    }
    dictionary2[0].word = dictionary[0].word;

    printf("%s\n",dictionary2[0].word);

}

int main (void) {
    struct entry dictionary[100] = 
    {{"aerie", "a high nest"},
    {"abyss", "a bottomless pit"},
    {"ahoy", "a nautical call of greeting"},
    {"addle", "to become confused"},
    {"aardvark", "a burrowing African mammal"},
    {"agar", "a jelly made of seaweed"},
    {"acumen", "mentally sharp; keen"},
    {"aigrette", "an ornamental cluster of feathers"},
    {"affix", "to attach"},
    {"ajar", "partially opened"}};
    dictionarySort(dictionary);
}

我收到以下错误消息:

error: array type 'char [15]' is not assignable
        dictionary2[0].word = dictionary[0].word;
        ~~~~~~~~~~~~~~~~~~~ ^

另一方面,如果我复制单个字符,我无法区分字符串,这在访问 dictionary2 时是必需的。

【问题讨论】:

    标签: c arrays string structure


    【解决方案1】:

    正如您的编译器告诉您数组在 c 中不可赋值,要复制字符串,您需要 strcpy() 函数

    strcpy(dictionary2[0].word[i], dictionary[0].word[i]);
    

    您需要确保目标数组有足够的空间来保存字符串,即nul 终止符的字符串+1 中的字符数。

    【讨论】:

      【解决方案2】:

      您可能想考虑使用 qsort(如果您有 linux/mac 类型 man qsort 了解更多信息)

      qsort 调用看起来像 qsort(dictionary,size_of_arr, sizeof(entry), func_name); 您必须创建一个名为 func_name 的比较函数(尽管您可以随意调用它,只要您在函数调用中正确传递它。

      int func_name(void *ent1, void *ent2)
      {
          struct entry a = *(struct entry*)ent1, b = *(struct entry*)ent2;
          return strcmp(a.word, b.word);
      }
      

      我认为这会起作用,如果不是接近它的东西会......

      【讨论】:

        【解决方案3】:

        你需要使用一个函数来分配字符串(字符数组也是如此)

        执行此操作的常用函数是:

        char a[15];
        strcpy( a , "ameer" ); % copying second string to first one
        

        来自库,意思是 a="ameer";

        所以在你的情况下你可以写:

        strcpy( dictionary2[0].word , dictionary[0].word );
        

        【讨论】:

          【解决方案4】:

          dictionary2[0].word[i] = dictionary[0].word[i]; 完全错误。您不能像 int 类型那样将一个数组复制到另一个数组中作为赋值操作。

          您可以使用 strcpy(或 strncpy)将字节从一个数组复制到另一个数组。或者使用 memcpy。

          strcpy(dictionary2[0].word[i], dictionary[0].word[i]); 
          
          
          
          memcpy(dictionary2[0].word[i],
          dictionary[0].word[i],sizeof(dictionary[0].word));
          

          【讨论】:

            【解决方案5】:

            您正在尝试使用赋值运算符“=”完全复制 1 个字符串。您可以在 for 循环中执行此操作,也可以使用 strcpy()。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-05-28
              • 1970-01-01
              • 1970-01-01
              • 2011-06-23
              • 1970-01-01
              相关资源
              最近更新 更多