简体   繁体   English

c结构数组

[英]c array of structures

This is a lot of code but I'm at ends here trying to figure out why its not working. 这是很多代码,但我在这里试图弄清楚为什么它不起作用。

This is the code I'm running: 这是我正在运行的代码:

struct dict {
  struct word **tbl;
  int (*hash_fcn)(struct word*);
  void (*add_fcn)(struct word*);
  void (*remove_fcn)(struct word*);
  void (*toString_fcn)(struct word*);
};

struct word {
  char *s;
  struct word *next;
};

this is part of the main function: 这是主要功能的一部分:

  hashtbl=malloc(sizeof(struct dict));
  //hashtbl->tbl=malloc(sizeof(struct word*)*256);
  int i;
  for(i=0;i<256;i++)
  {
    hashtbl->tbl[i] = malloc(sizeof(struct word));
    hashtbl->tbl[i] = NULL;
    //hashtbl->tbl[i]=NULL;
  }
  hashtbl->add_fcn=&add_word;
  hashtbl->remove_fcn=&remove_word;
  hashtbl->hash_fcn=&hash_function;
  hashtbl->toString_fcn=&toString;

  FILE *p;
  p = fopen("a.txt", "r");
  if(p == NULL) { printf("ERROR!\n"); return 0; }
  char line[MAXBYTES];
  while(fgets(line, MAXBYTES, p))
  {
      char *token = strtok(line, " ");
      while(token != NULL)
     {
        struct word *words = malloc(sizeof(struct word));
        words->s = token;
        words->next=NULL;
        trim(words);
        hashtbl->add_fcn(words);
        token = strtok(NULL, " ");

        printf("....\n");
        hashtbl->toString_fcn(NULL);
        printf("....\n\n");

      }
      free(token);
  }

Here are two functions used (to String just prints it out) 这里有两个使用的函数(String只打印出来)

void add_word(struct word *insert)
{
  if(strcmp(insert->s, "\n") == 0) {return;}

  int hash = hashtbl->hash_fcn(insert);

  if(hash==0) { return; }
  struct word *word = hashtbl->tbl[hash];
  if(word==NULL)
  {
    struct word *newword = malloc(sizeof(struct word));
    newword->s=insert->s;
    newword->next=NULL;
    hashtbl->tbl[hash]=newword;

    printf("() %d %s \n", hash, hashtbl->tbl[hash]->s);
  }
  else
  {
    struct word *tp = word;
    while(word->next != NULL)
      word=word->next;
    struct word *newword = malloc(sizeof(struct word));
    newword->s=insert->s;
    newword->next=NULL;
    word->next=newword;
    hashtbl->tbl[hash]=tp;
  }
}

int hash_function(struct word *string)
{
  char *firstletter = string->s;
  char c = *firstletter;
  int ascii = (int)c;
  return ascii;
}

a.txt is a.txt是

cat
dog
mouse

and it prints out the following: 它打印出以下内容:

() 99 cat 
....
99 : cat 
....

() 100 dog 
....
99 : dog 
100 : dog 
....

() 109 mouse 
....
99 : mouse 
100 : mouse 
109 : mouse 
....

it should be printing out 它应该打印出来

99:cat 
100:dog
109:mouse

thanks 谢谢

You have many problems here. 你这里有很多问题。

First: 第一:

hashtbl=malloc(sizeof(struct dict));
//hashtbl->tbl=malloc(sizeof(struct word*)*256);

You need to uncomment that line, since without it you don't allocate the array of struct pointers in hashtbl->tbl , and leave it uninitialized. 你需要取消注释该行,因为没有它你不会在hashtbl->tbl分配struct指针数组,并保持未初始化。 It'll probably cause a segfault as is. 它可能会导致段错误。

Next: 下一个:

for(i=0;i<256;i++)
{
  hashtbl->tbl[i] = malloc(sizeof(struct word));
  hashtbl->tbl[i] = NULL;
  //hashtbl->tbl[i]=NULL;
}

This is leaking memory. 这是泄漏的记忆。 You're allocating a struct word to each element of hashtbl->tbl , but then overwriting the pointer with NULL . 您正在为hashtbl->tbl每个元素分配一个struct word ,但随后用NULL覆盖指针。 So you leak all the memory you allocated, and leave each array element set to NULL . 所以你泄漏了你分配的所有内存,并将每个数组元素设置为NULL Get rid of the malloc() and just set them to NULL , since you'll allocate the structs later when you actually add a word. 摆脱malloc()并将它们设置为NULL ,因为稍后在实际添加单词时将分配结构。

In this part: 在这部分:

hashtbl->add_fcn=&add_word;
hashtbl->remove_fcn=&remove_word;
hashtbl->hash_fcn=&hash_function;
hashtbl->toString_fcn=&toString;

...the & are not needed -- the function names without parentheses are good enough. ... &不需要 - 没有括号的函数名称就足够了。 As long as the parentheses are omitted, it will be interpreted as the address of the function and not a function call. 只要括号被省略,它将被解释为函数的地址而不是函数调用。

And then here: 然后在这里:

    char *token = strtok(line, " ");
    while(token != NULL)
   {
      struct word *words = malloc(sizeof(struct word));
      words->s = token;

...the char * returned by strtok() typically points within the string you're processing, ie within the buffer you read lines into. ... strtok()返回的char *通常指向您正在处理的字符串,即在读取行的缓冲区内。 It will not remain intact while you continue processing the file. 继续处理文件时,它不会保持不变。 You need to make a copy of the string, not just save the pointer. 您需要复制字符串,而不仅仅是保存指针。

    free(token);

...are you sure you should free this? 你确定你应该释放这个吗?

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

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