将 Struct 设置为等于返回相同结构的函数

发布于 2025-01-04 05:45:30 字数 1062 浏览 3 评论 0原文

所以我正在进行项目的最后一步,我必须从函数返回一个结构,这样我就可以在 main 中使用它。 可以看到相关代码:

typedef struct SomeProduct {
    int itemNum;
    char itemName[21];
    double unitPrice;
    int stockQty;
    int restockQty;
    struct SomeProduct *next;
} SProduct;

 struct llpro {

 SProduct data;
 struct llpro *next;
 };

////////////////////SKIP LINES to identifier

SProduct findItem(struct llpro *head,int num);

///////////////////SKIP LINES to assignment that fails. head is the proper
////////////////// pointeritemspurchased is an int.

SProduct steve;
steve=findItem(head,newv.itemsPurchased[y]);

//////////////////Skip Lines to method

SProduct findItem(struct llpro *head,int num)
{
    while(head!=NULL)
    {
       if(head->data.itemNum==num)
       {
         SProduct paul;
         paul=head->data;
         return paul;
       }
    }
}

每当我尝试编译它时,我都会收到链接器错误,指出它们从未定义。然后,当我取出标识符时,我收到一条消息,指出 steve 和 paul 是不兼容的类型,即使它们都是 SProducts。请帮忙! 我还想澄清一下,我想做的是搜索 Sproducts 的链接列表,并从与我正在搜索的产品共享项目编号的产品中提取信息。链接器错误显示“函数 printsum 中对 'finditem' 的未定义引用

So I'm on the last steps of my project, and I have to return a struct from a function, so I can use it in main.
Relevant code can be seen:

typedef struct SomeProduct {
    int itemNum;
    char itemName[21];
    double unitPrice;
    int stockQty;
    int restockQty;
    struct SomeProduct *next;
} SProduct;

 struct llpro {

 SProduct data;
 struct llpro *next;
 };

////////////////////SKIP LINES to identifier

SProduct findItem(struct llpro *head,int num);

///////////////////SKIP LINES to assignment that fails. head is the proper
////////////////// pointeritemspurchased is an int.

SProduct steve;
steve=findItem(head,newv.itemsPurchased[y]);

//////////////////Skip Lines to method

SProduct findItem(struct llpro *head,int num)
{
    while(head!=NULL)
    {
       if(head->data.itemNum==num)
       {
         SProduct paul;
         paul=head->data;
         return paul;
       }
    }
}

Anytime I try to compile it I get linker errors saying that they are never defined. Then when I take out the identifier, I get a message saying that steve and paul are incompatible types, even though they are both SProducts. Please help!
Id also like to clarify, what im trying to do is search through a linked list of Sproducts, and pull the information from the one that shares the item number with the one im searching for. The linker error says "undefined reference to 'finditem' in function printsum

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰火雁神 2025-01-11 05:45:30

此代码在 GCC 4.1.2 下使用选项 -Wall -Wextra -std=c99 进行编译:

#include <stddef.h>

typedef struct SomeProduct
{
    int                 itemNum;
    char                itemName[21];
    double              unitPrice;
    int                 stockQty;
    int                 restockQty;
    struct SomeProduct *next;
} SProduct;

struct llpro
{
    SProduct data;
    struct llpro *next;
};

SProduct findItem(struct llpro *head,int num);

int main(void)
{
    struct llpro *head = 0;
    SProduct steve;
    steve = findItem(head, 1);
}

SProduct findItem(struct llpro *head, int num)
{
    while (head != NULL)
    {
        if (head->data.itemNum == num)
        {
            SProduct paul;
            paul = head->data;
            return paul;
        }
    }
    SProduct alan = { 0, "", 0.0, 0, 0, 0 };
    return alan;
}

主要更改是使用 1 代替 -Wall -Wextra -std=c99 参数的复杂值code>findItem() 并在 findItem() 末尾添加 return。两者都不影响任何分配。

因此,假设您遇到问题,那么您错误地摘录了代码,并且没有向我们显示导致问题的行。

This code compiles under GCC 4.1.2 with options -Wall -Wextra -std=c99:

#include <stddef.h>

typedef struct SomeProduct
{
    int                 itemNum;
    char                itemName[21];
    double              unitPrice;
    int                 stockQty;
    int                 restockQty;
    struct SomeProduct *next;
} SProduct;

struct llpro
{
    SProduct data;
    struct llpro *next;
};

SProduct findItem(struct llpro *head,int num);

int main(void)
{
    struct llpro *head = 0;
    SProduct steve;
    steve = findItem(head, 1);
}

SProduct findItem(struct llpro *head, int num)
{
    while (head != NULL)
    {
        if (head->data.itemNum == num)
        {
            SProduct paul;
            paul = head->data;
            return paul;
        }
    }
    SProduct alan = { 0, "", 0.0, 0, 0, 0 };
    return alan;
}

The main changes are using 1 in place of your complex value for the argument to findItem() and the addition of a return to the end of findItem(). Neither affects any assignments.

So, assuming you have problems, you've mis-excerpted your code and not shown us the lines causing the trouble.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文