简体   繁体   English

将char数组传递给struct成员

[英]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. 在下面的函数中,我采用了一个我想分配给结构的char指针(char数组)参数。

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. 第62行和第63行是`newItem - > ...“行。

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. 你几乎肯定不想只将char *分配给char [] - 正如编译器指出的那样,类型是不兼容的,语义不是你想的。 I assume you want the struct members to contain the values of the two char* strings - in which case, you want to call strncpy. 我假设您希望struct成员包含两个char *字符串的值 - 在这种情况下,您要调用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复制字符串的内容,如Adam所示:

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. 当您将指针传递给函数时,您将传递一个内存地址(一个整数),指示可以找到以null结尾的字符串的位置。

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: 例如,这段代码很糟糕,因为传递给insertItem的字符串在fillStructure返回后不再存在:

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. 更多,我建议阅读C FAQ中的“数组和指针”章节 - 从问题6.2开始并继续阅读。

You should chang your struct in 你应该改变你的结构

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

to assign a char pointer to a name. 为名称指定一个char指针。 In the struct you defined char name[30] are just 30 chars. 在结构中,您定义的char name [30]只有30个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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