【问题标题】:I am having trouble assigning a char string to a char array我无法将 char 字符串分配给 char 数组
【发布时间】:2012-10-16 03:29:24
【问题描述】:

这就是我现在所拥有的。

void insert(char Table[][81], char Key[81]){
    int index;
    index = search(Table, Key); 
    // This is another function used to find an empty 'slot'
    // in the table
    if(Table[index] == '\0')
    Table[index] = Key; 
    // <-- This is the line that contains some sort of error.
    else
    printf("ERROR: Key already in Table\n");
}

它抛出的错误是:

从类型“char *”分配给类型“char[81]”时类型不兼容。

我不知道如何修复或抛出此错误的原因。如果有人需要我的程序中的更多信息来得出结论,请告诉我。

【问题讨论】:

    标签: c arrays char assign


    【解决方案1】:

    您不能分配数组,但可以使用 strcpy()memcpy() 代替:

    if (Table[index][0] == '\0')
        memcpy(Table[index], Key, sizeof(Key));
    else
        printf("ERROR: Key already in Table\n");
    

    【讨论】:

      猜你喜欢
      • 2011-02-07
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多