I am writing a code where I have to add a value to an abstract data table, but I am not sure why I cannot as it shows “error C2106: ‘=’ : left operand must be l-value” error.
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;
}
I did some search online, but could not find too relative solution for me.
I would really appreciate any hint on that.
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;
}
}
The fields
indexandotherare arrays, you can’t assign arrays. You’d have to copy them withmemcpy.Another option would to have
top_addreceivepairinstead of the two separately. Then you could assign thestruct.