c - 在 C 中为结构指针赋值时遇到问题?

标签 c pointers struct compiler-errors

我正在构建一个具有结构的链表字典,列表中的每个节点定义如下:

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/

相关文章:

c - 如何加速指针取消引用?

c++ - 如何覆盖 boost::serialize 获取指向对象的指针时发生的情况

C 编程 : Reading from a file and printing on screen - formatting

c - 如何正确分配新的字符串值?

c - C 预处理器警告/错误消息中的字符串插值

c - Tilde C 无符号整数与有符号整数

c - 栈的链表实现

Java JNI 和 Vala - undefined symbol : g_once_init_enter

c - 仅使用加法/减法编写模函数

c++ - 如何在 C++ 中将文本文件解析为结构的不同元素?