简体   繁体   English

在C中为struct指针赋值?

[英]Trouble with assigning values to struct pointers in C?

I'm constructing a linked list dictionary with a structure, with each node in the list defined as the following: 我正在构建一个带有结构的链表字典,列表中的每个节点都定义如下:

typedef struct node node;
struct node
{                                                               
      int key;
      char value[ARRAY_MAX];
      node *next;
};  

Where I'm running into problems is when I'm assigning values to key or value within my insert and makedict functions. 我遇到问题的地方是我在insert和makedict函数中为key或value赋值。 I received the following errors in assignment: 我在分配时收到以下错误:

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

and: 和:

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

I know I'm probably missing something incredibly obvious, but I've been reading this code over and over to no avail. 我知道我可能错过了一些非常明显的东西,但我一直在读这段代码而无济于事。 Any help is appreciated. 任何帮助表示赞赏。

I think the line 我觉得行

inputnode->value = vinput;

is what the compiler is complaining about. 是编译器抱怨的。 Try 尝试

strcpy(inputnode->value, vinput);

Or, better yet, make the 'value' field a char * and do 或者,更好的是,将'value'字段设为char *并执行

inputnode->value = strdup(vinput)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM