Passing char array to struct member
c, struct
Solution
You almost certainly don't want to just assign the char* to the char[] - as the compiler points out, the types are incompatible, and the semantics are not what you think. I assume you want the struct members to contain the values of the two char* strings - in which case, you want to call strncpy.
strncpy(target, source, max_chars);
Problem
I have the following structure: ``` struct hashItem { char userid[8]; char name[30]; struct hashItem *next; }; ``` In the function below I take a char pointer (char array) argument that I wish to assign to the struct. ``` void insertItem(struct hashItem *htable[], char *userid, char *name) { int hcode = hashCode(userid); struct hashItem *current = htable[hcode]; struct hashItem *newItem = (struct hashItem*) malloc(sizeof(struct hashItem)); newItem->userid = userid; newItem->name = name; [...] } ``` Instead I get the following error: ``` hashtable.c: In function ‘insertItem’: hashtable.c:62: error: incompatible types in assignment hashtable.c:63: error: incompatible types in assignment ``` Line 62 and 63 are the `newItem->..." lines.