简体   繁体   English

C:将元素插入到结构数组中

[英]C: Inserting element into an array of structs

I have an array of structs where each array element is:我有一个结构数组,其中每个数组元素是:

struct Item {
  int code;
  char * label;
};

The array itself is a global variable:数组本身是一个全局变量:

struct Item * ht[SIZE];

This is how I currently insert an item into the array:这就是我目前向数组中插入一个项目的方式:

void insert(int toadd, char *toput) {

   struct Item *item = (struct Item*) malloc(sizeof(struct Item));
   item->label = toput;  
   item->code = toadd;

   int hashIndex = 0; 

   //move in array until an empty or deleted cell
   while(ht[hashIndex] != NULL && ht[hashIndex]->code != -1) {
      //go to next cell
      ++hashIndex;

      //wrap around the table
      hashIndex %= SIZE;
   }

   ht[hashIndex] = item;
}

In another function, I call the insert method, followed with some printf statements to check what's going on:在另一个函数中,我调用了 insert 方法,然后是一些 printf 语句来检查发生了什么:

insert(ctr, trimwhitespace(line2));
printf("\nAdding to ht: String: %s Integer: %d\n", trimwhitespace(line2), ctr);

for (int i = 0; i < SIZE; i++) {
    if (ht[i] != NULL)
    printf("\nThis is what's inside ht: String: %s Integer: %d\n", ht[i] -> label, ht[i] -> code);            
}

This is an example of the output:这是输出的示例:

Adding to ht: String: four Integer: 6添加到 ht: String: 4 Integer: 6

This is what's inside ht: String: four Integer: 0这是 ht 里面的内容: String: 4 Integer: 0

This is what's inside ht: String: four Integer: 4这是 ht 里面的内容: String: 4 Integer: 4

This is what's inside ht: String: four Integer: 5这是 ht 里面的内容: String: 4 Integer: 5

This is what's inside ht: String: four Integer: 6这是 ht 里面的内容: String: 4 Integer: 6

As you can see, the struct is being inserted multiple times, with different integer values.如您所见,该结构被多次插入,具有不同的整数值。

I think this is unlikely to be an issue with the loops that the insert call is in, as the first print statement would also be printed multiple times if the insert call was being made multiple times.我认为这不太可能是insert调用所在的循环的问题,因为如果多次进行insert调用,第一个打印语句也将被打印多次。 But I may be wrong.但我可能错了。

How do I make sure that the insert method only inserts the struct once and not multiple times?我如何确保insert方法只插入一次结构而不是多次? Or does the problem lie elsewhere?还是问题出在其他地方?

Turns out that adding an if statement right before my insert method call, to check whether the particular key was already inserted earlier, solved the problem, though this may not be the most ideal fix:事实证明,在我的insert方法调用之前添加一个if语句,以检查特定键是否已在之前插入,解决了问题,尽管这可能不是最理想的解决方法:

if (!containsKey(trimwhitespace(stringcopy3))) {           

      insert(ctr, trimwhitespace(stringcopy3));
      printf("\nAdding to ht: String: %s Integer: %d\n", trimwhitespace(stringcopy3), ctr);

}

I'm still not sure why multiple instances of the key were being inserted in the first place, but this is a temporary solution.我仍然不确定为什么首先插入密钥的多个实例,但这是一个临时解决方案。

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

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