我正在构建一个具有结构的链表字典,列表中的每个节点定义如下:
typedef struct node node;
struct node
{
int key;
char value[ARRAY_MAX];
node *next;
};
当我在 insert 和 makedict 函数中为键或值赋值时,我遇到了问题。我在作业中收到以下错误:
node* insert(node* start, char* vinput, int kinput) {
node* temp = start;
while((temp->next->key < kinput) && temp->next!=NULL) {
temp=temp->next;
}
if(temp->key==kinput) {
temp->key = kinput;
return temp;
} else {
node* inputnode = (node*)malloc(sizeof(node));
inputnode->next = temp->next;
temp->next = inputnode;
inputnode->key = kinput; /*error: incompatible types in assignment*/
inputnode->value = vinput;
return inputnode;
}
和:
node* makedict(char* vinput, int kinput) {
node* temp = (node*)malloc(sizeof(node));
temp->value = vinput;
temp->key = kinput; /*error: incompatible types in assignment*/
temp->next = NULL;
return temp;
}
我知道我可能遗漏了一些非常明显的东西,但我一遍又一遍地阅读这段代码却无济于事。感谢您的帮助。
最佳答案
我觉得行
inputnode->value = vinput;
是编译器提示的。尝试
strcpy(inputnode->value, vinput);
或者,更好的是,将“值”字段设为 char * 并执行
inputnode->value = strdup(vinput)
关于c - 在 C 中为结构指针赋值时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18583177/