开发者社区> 问答> 正文

C中的返回结构指针

您能否帮助我更正我的代码:

这个函数返回一个指向结构项的指针:

struct item* findItem(const char* key) {
    for (int i = 0; i < nItems; i++) {
        if (!strcmp(items[i].key, key)) { return &items[i]; }
    }
    return NULL;
}

从主要功能,我想检索我的结构值,如下所示:

struct item search_items = findItem(&key) ; // I have problem with this line 
char* itemValue;
if (search_items != NULL)
{
    itemValue = search_items->value;
}

如何检索结构并将其保存以在main功能中使用?

展开
收起
游客ufivfoddcd53c 2020-01-04 19:11:57 920 0
1 条回答
写回答
取消 提交回答
  • 如果要从函数返回指针,则必须将其作为指针读取。注意struct item* search_items我的代码和您的部分(我添加了指针*)

    struct item* search_items = findItem(&key) ; // i have problem with this line 
    char* itemValue;
    if ( search_items != NULL)
    {
        itemValue = search_items->value;
    }
    
    2020-01-04 19:12:11
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载