Refering to structure in structure, in a function

In ANSI C, I'm trying to add an item to the end of a linked-list, using the following structures:

typedef struct items{
    char itemname[30];
    int damage;
    int defense;
}items;


typedef struct itemlist{
    struct items item;
    struct itemlist *next;
} itemlist;

In short, itemlist is a "cell" struct in the list, and items is what contains the data. I try to refer to them as this:

itemlist* additem(itemlist *itemslist, items data){
   itemlist *moving, *new;

   new = (itemlist*) malloc(sizeof(itemlist));

   /* These 3 lines are not working*/
   strcpy(new->item->itemname,data->itemname);
   new->item->damage = data->damage;
   new->item->defense = data->defense;

   new->next = NULL;

   if (itemslist == NULL)     /* empty list? */
      return new;

   for (moving = itemslist; moving->next != NULL; moving = moving->next); 

   moving->next = new;

   return itemlist;
}

My question is, how do I refer to these structure in structure types? The error messages read as:

error: invalid type argument of '->' (have 'struct items')

error: invalid type argument of '->' (have 'items')

Thank you for your time

Solutions

The item in itemlist is not a pointer to an item but the actual item so you don't use -> you use . as in.

new->item.damage

Same with the function argument data.

As an aside, it's generally a bad idea to use C++ keywords like new in C code. If you ever want to compile this with a C++ compiler it's going to be a pain. Or worse, if you wind up with C++ keywords in your headers then you can't even expose the header to a C++ application.

The item data is passed by value to the function so you need to use the . operator instead of -> here.

Tags: C / Function / Pointers

Similar questions

Refering to an Array. Incrementing and saving index
Learning C through "Learning C the hard way", and doing some of my own exercises. I stumbled upon the following problem. Let's say I have the following structure: In main(), I have declared the following array: Now let's say 2 functions away (main calls function1 which calls function2) I want to save a person inside the array I declared in the main...
In c: Can I copy a whole portion of an array at once, by refering to the pointer to the location of a slot in the array I want to copy from?
Hope my question is clear and relavent, new to Pointers... - Can I copy a whole portion of an array at once, by refering to the pointer to the location of the first slot in the array I want to begin copying from? For example - Given an array : A [ 1,2,3,4,5,7,8,3,2,5,1,0,9] - I want to copy only the part of the array from the n'th slot on, into the...
Structures refering to each other
I have seen questions talking about structs that have pointers to each other, I tried a lot of things but I just can't solve my problem. My brain is just, burning! I have two different structures/types: set_t and node_t I don't have any node_t in my set_t structure, but I use node_t as parameters, so I include node.h as you can see. List is a linke...
What really is the difference between data types when refering to memory?
I take a course in machine oriented programming and I have some problem understanding data types when referring to a memory address. For example, if I want to access a piece of memory on position 0x40020000. I would have written it like this. But what is the difference between using some other data type? For example unsigned short or unsigned char....
How to import a haskell module that uses FFI without refering to the c object?
I'm trying to write a haskell module that wraps a bunch of c functions. I want to be able to import this module like any other haskell module without referring to the c object files. I can't find any examples about how to do this. This is what I've tried. I have a c file "dumbCfunctions.c": and a haskell file with a module defined in it "Callfuncti...
Final size of a structure variable of data structure "stack" (implementation through structure and its creation through function)
So I am learning about data structure stack in my college, howsoever I have some doubts in its implementation and the memory it occupies. Below is a small code which I have written for it: - Here I have defined Stack through struct stack as seen below in this portion of the code: - Now the memory which would be occupied by the above struct stack sh...
C

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.