Reputation: 23
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
Upvotes: 2
Views: 750
Reputation: 753900
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.
Upvotes: 1