简体   繁体   English

将指针传递给结构数组

[英]Passing a pointer to an array of structures

I am having issues with a book assignment that I have been working on, hoping to get some help. 我一直在从事的书籍作业遇到问题,希望能获得一些帮助。 My program compiles without issue, but when I run it, I get 我的程序编译没有问题,但是当我运行它时,我得到了

First time through: (null)
After: 0000000
First time through: (null)
After: 1111111

When I insert a hash it checks to see if that array value has already been assigned. 当我插入一个哈希值时,它将检查是否已分配该数组值。 If it does, it prints "Collision detected" and it creates a new node and links it to the previous node. 如果是这样,它将打印“检测到冲突”,并创建一个新节点并将其链接到上一个节点。 Right now it seems like its an automatic variable even though I am passing it a pointer? 现在,即使我向它传递了一个指针,它似乎还是一个自动变量?

I am pretty sure it has to do with how I am declaring my array of structures and passing them into insert_hash, but I cant seem to figure it out. 我很确定这与我声明结构数组并将它们传递给insert_hash的方式有关,但是我似乎无法弄清楚。 Any help is appreciated! 任何帮助表示赞赏!

struct hash_node
{
    char *data;
    struct hash_node *next;

};

struct hash_node *create_table(int size);
unsigned int hash(const char *str);
void insert_hash(const char *str, const char *value, struct hash_node *h);

int
main(void)
{   
    struct hash_node *table = create_table(101);

    insert_hash("testing", "0000000", table);
    insert_hash("testing", "1111111", table);


}

struct hash_node
*create_table(int size)
{
    struct hash_node *table = calloc(size, sizeof(*table));

    if(!table)
    {
        fprintf(stderr, "Unable to allocate memory\n");
        return NULL;
    }

    return table;
}

unsigned int
hash(const char *str)
{
    unsigned int c, hash;

    while ((c = *str++))
    {
        hash += c;
    }

    hash = hash % 100;

    return hash;
}

void
insert_hash(const char *str, const char *value,  struct hash_node *h)
{
    int temp = hash(str);

    printf("First time through: %s\n", h[temp].data);

    if (h[temp].data)
    {
        fprintf(stderr, "Collision detected\n");

        struct hash_node *node = calloc(1, sizeof(*node));

        if (!node)
        {
            fprintf(stderr, "Unable to allocate memory\n");
            return;
        }

        node -> data = malloc(strlen(value) + 1);
        strncpy(node -> data, value, strlen(value) + 1);

        node->next = NULL;

        h[temp].next = node;
    }
    else
    {

        h[temp].data = malloc(strlen(value) + 1);
        strncpy(h[temp].data, value, strlen(value) + 1);
    }

    printf("After: %s\n", h[temp].data);

}

The hash function is wrong hash函数错误

unsigned int
hash(const char *str)
{
    unsigned int c, hash;   //<--- not initialized

    while ((c = *str++))
    {
        hash += c;
    }

    hash = hash % 100;

    return hash;
}

The hash variable is not initialized, so it has an undefined value every time you execute it. hash变量初始化,因此每次执行时都具有未定义的值。 Initialize it with 0: 用0初始化:

unsigned int
hash(const char *str)
{
    unsigned int c, hash = 0;

    while ((c = *str++))
    {
        hash += c;
    }

    hash = hash % 100;

    return hash;
}

Then I get 然后我得到

First time through: (null)
After: 0000000
First time through: 0000000
Collision detected
After: 0000000

edit 编辑

In case of a collision you are also not appending the new node correctly. 如果发生冲突,您也不会正确附加新节点。 You first have to find the end of the list and then append the new node at the end of the list: 您首先必须找到列表的末尾,然后将新节点追加到列表的末尾:

node *tail = h[temp].next;

while(tail->next)
    tail = tail->next;


struct hash_node *node = calloc(1, sizeof(*node));
...

tail->next = node;

Or you can preprend the new node at the beginning of the list 或者,您可以将新节点放在列表的开头

struct hash_node *node = calloc(1, sizeof(*node));
...

node->next = h[temp].next;
h[temp].next = node;

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

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