Sorry if the title is a little confusing. What I’m doing is creating a structure such as:
struct record
{
int value;
int key;
};
Then using a typedef to call a pointer to record “Item” like this:
typedef struct record* Item;
Basically I’m following how it was done in Algoriths in C by Robert Sedgewick (third edition) on page 290 in case anyone happens to have this book.
What I’m having trouble with is reading in a value from the console, then assigning that to the key. Here’s what I have, and the errors that I’m getting:
void setKey(Item *element, int x)
{
element->key = x;
}
void standInput(Item A[], int length)
{
int i;
int x;
for(i = 0; i < length; i++)
{
printf("Enter a value for spot %i: ", i+1);
scanf("%d", &x);
setKey(A[i], x);
}
}
gcc Item.h
Item.h:33:6: warning: conflicting types for ‘setKey’
Item.h:23:3: note: previous implicit declaration of ‘setKey’ was here
If I could get a nudge in the right direction, I’d really appreciate it. I got the program for this assignment working perfectly when Item was just simple ints, but now I’m trying to use Item->Key and I’m a little lost 🙂 Thanks!
If anyone needs any other portion of the code that I didn’t think necessary, I’ll post it as soon as I see the request.
Revision: I moved my setKey function above standInput, so the compilation error has gone away. What I am getting though is a segment fault, so I’m still assigning it wrong 🙂
The type of
setKey‘s first argument should beItem, notItem *, since theItemtype already is a pointer. You want to pass a pointer to a record, not a pointer to a pointer to a record.