加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C中为struct指针赋值?

发布时间:2020-12-16 09:47:16 所属栏目:百科 来源:网络整理
导读:我正在构建一个带有结构的链表字典,列表中的每个节点都定义如下: typedef struct node node;struct node{ int key; char value[ARRAY_MAX]; node *next;}; 我遇到问题的地方是我在insert和makedict函数中为key或value赋值.我在分配时收到以下错误: node* i
我正在构建一个带有结构的链表字典,列表中的每个节点都定义如下:

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

我遇到问题的地方是我在insert和makedict函数中为key或value赋值.我在分配时收到以下错误:

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

或者,更好的是,将’value’字段设为char *并执行

inputnode->value = strdup(vinput)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读