错误C2106:’=’:左操作数必须是C中的l值

我正在编写一个代码,我必须在抽象数据表中添加一个值,但我不知道为什么我不能,因为它显示“错误C2106:’=’:左操作数必须是l值”错误。

int top_add(top_string *table, const char index[257], const char other[257]) { top_remove(&table, index); if (table->item_count == table->size) { printf("/n Table is full."); return -1; } /* error C2106: '=' : left operand must be l-value */ table->item[table->item_count].index = index; /*error C2106: '=' : left operand must be l-value */ table->item[table->item_count].other = other; table->item_count++; return 1; } 

我在网上做了一些搜索,但找不到太相关的解决方案。

我真的很感激任何提示。

更新:

 typedef struct { char index[257]; char other[257]; } pair; typedef struct { pair *item; int item_count; int size; } top_string; int top_init(top_string *table, const int size) { table->item = malloc((size+1)*sizeof(top_string)); table->size = size; table->item_count = 0; if (table->item == NULL) { return 0; /* failed to allocate memory */ } else { return 1; } } 

字段indexother是数组,您不能分配数组。 你必须用memcpy复制它们。

另一个选择是分别使用top_add接收pair而不是两个。 然后你可以分配struct

除了分配数组的问题之外,还需要取消引用指针table-> item。 子索引[table-> item_count]必须在.index之后

 ((table->item)->index)[table->item_count] 

这是一个l值

您无法分配给arrays。 你必须用strncpy,memcpy或其他一些东西复制到它们中。