2011-11-07 60 views
2

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

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

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

我会很感激的任何提示。

UPDATE:

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

} 
+0

后top_string –

+0

的定义,请告诉我们什么'表 - >项目[表 - > ITEM_COUNT ] .index'和'table-> item [table-> item_count] .other'实际上是。 – m0skit0

+0

'table-> item [table-> item_count] .index'的声明是怎么样的?如果它也是一个数组,那么它可以被分配,你必须通过一些函数复制数据(比如'memcpy')。同样适用于'table-> item [table-> item_count] .other'。 –

回答

4

字段indexother是数组,你不能分配数组。您必须将其复制到memcpy

另一种选择是有top_add收到pair而不是两个分开。然后你可以分配struct

0

您不能分配到阵列。您必须使用strncpy,memcpy或其他类型复制到它们中。

1

除了分配阵列的问题,则需要取消引用指针表 - >项目。而分类指数[表 - > ITEM_COUNT]必须经过的.index

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

这是一个L值

相关问题