简体   繁体   English

C-指向结构体的指针的指针

[英]C - pointer to struct to array of pointers

I have a linked list with a hash table in each node. 我在每个节点中都有一个带有哈希表的链表。 The hash table is implemented by an array of pointers to structs. 哈希表由指向结构的指针数组实现。 The whole management of this is made by a global static pointer to the linked list. 整个管理是通过指向链接列表的全局静态指针进行的。

  • I changed a little bit the question! 我改变了一点问题! Now the question is more focused. 现在这个问题更加集中。

in the lookup and insert function to make the code shorter I assign 在查找和插入功能中,使代码更短,我分配

temp = cur_table->symbols_table[entry];

but I see that temp gets NULL all the time. 但我看到temp一直都为NULL。 I can't understand why is that happens? 我不明白为什么会这样?

The code is below in 3 modules. 以下3个模块中的代码。 Thank you in ahead. 谢谢你在前面。

symbols.h file: symbol.h文件:

#include <stdlib.h>
#include <stdio.h>

#define TABLE_SIZE 26

typedef struct symbol_node
{
  char* name;
  int type;
  int role;

  struct symbol_node* next;

} symbol_node;

typedef struct table_node
{
  struct symbol_node* symbols_table[TABLE_SIZE];
  struct table_node* prev;
  struct table_node* next;

} table_node;


static struct table_node* cur_table;

//functions declarations:
void init_table();
int hash_function(char* id);
symbol_node* lookup(char* id_name);
symbol_node* insert(char* id_name);
// debug
void printtable();

symbols.c 符号

 void init_table() // creates the first node                                                                                                                                                                        
 {
  int i = 0;
  cur_table = NULL;

  cur_table = (table_node*)malloc(sizeof(table_node));

  cur_table->prev = NULL;
  cur_table->next = NULL;

  for(i=0; i < TABLE_SIZE; i++)
    {
      cur_table->symbols_table[i] = NULL;
    }
}

symbol_node* lookup(char* id_name)  // returns null if the id name not found                                                                                                                                       
{
  symbol_node* result = NULL;
  symbol_node* temp = NULL;
  int entry = atoi(id_name);

  temp = cur_table->symbols_table[entry];

  while(temp != NULL)
    {
      if( strcmp( id_name, temp->name ) == 0 )
    {
          result = temp;
          break;
        }
      else
        temp = temp->next;
    }

  return result;
}


symbol_node* insert(char* id_name)
{
  symbol_node* result = NULL;
  symbol_node* temp = NULL;
  int index = -1;

  if(lookup(id_name)==NULL)
    {
      index = atoi(id_name);
      temp = cur_table->symbols_table[index];

      while(temp!=NULL)
        {
          temp = temp->next;
        }

      temp = (symbol_node*)malloc(sizeof(symbol_node));
      temp->next = NULL;
      temp->name = id_name;
      // TODO: other params                                                                                                                                                                                        

      result = temp;
    }

  return result;
}
void printtable()
{
int i=0;

for(i=0; i<TABLE_SIZE; i++)
{
    if(cur_table->symbols_table[i]==NULL)
        printf("NULL at index %d\n",i);
    else
        printf("There are something\n");
}
}

main.c main.c

void main()
{
int i=0;
symbol_node* t = NULL;
symbol_node* tt = NULL;
init_table();

t = insert("markhit");
t = insert("mark");

tt = lookup("mark");

printtable();

_getch();

  free(t);
  free(tt);
  free(cur_table);
}

avoid memory allocation [`malloc'] statically. 静态避免内存分配[`malloc']。 try it 试试吧

cur_table = new table_node;

for statically allocated memory, you can not set your value for memory reason. 对于静态分配的内存,由于内存原因,您无法设置值。 when you are inserting it is not reallocating your cur_table 当您插入时,它不会重新分配您的cur_table

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

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