简体   繁体   中英

Passing char array to struct member

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.

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);

You can't assign a pointer to a string to a character array like you are trying to. Instead you need to copy the contents of the string with strncpy as Adam indicated:

strncpy (newItem->userid, userid, 8);

When you declare the struct with a character array in it, you are allocating memory inside the structure itself to store a string of the given length.

When you pass a pointer into your function, you are passing a memory address (an integer) that indicates where a null-terminated string can be found.

To assign the pointer to the array doesn't make sense. The array has memory allocated for it already -- it can't be made to "point" to another location.

While you can use pointers in your structure, you need to be very careful that when you assign them, you are telling them to point to something that is going to be valid for the duration of the time you will use the structure. For example, this code is bad, because the string passed to insertItem no longer exists after fillStructure returns:

struct hashItem
{
   char * userid;
};

void insertItem (struct hashItem * item, char * userid)
{
   item->userid = userid;
}

void fillStructure (struct hashItem * item)
{
   const char string[] = "testing";
   insertItem (item, string);
}

int main(void)
{
   struct hashItem item;
   fillStructure (&item);
   /* item->userid is now a dangling pointer! */
}

For more, I would recommend reading the "Arrays and Pointers" chapter of the C FAQ -- start with Question 6.2 and keep reading from there.

You should chang your struct in

struct hashItem {
  char userid[8];
  char *name;
  struct hashItem *next;
};

to assign a char pointer to a name. In the struct you defined char name[30] are just 30 chars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM