简体   繁体   English

为什么我的用于将元素插入到哈希树中的C代码在Main()中起作用,但是当我通过函数调用它时却不起作用?

[英]Why does my C code for inserting an element into a hash tree work in Main() but not when I call it via function?

I'm fairly certain this has something to do with pointers and the function using copies instead, but I'm not sure how...because I've inserted the pointer as a parameter for create(); 我相当确定这与指针和使用副本的函数有关,但是我不确定如何...因为我已经将指针作为create()的参数插入了;

#include <stdio.h>
#include <cs50.h> 
#include <string.h>

typedef struct list {
    string word;
    struct list *next;
}
linkedList; 

struct list* create (string newWord) {
    linkedList *new = malloc(sizeof(newWord));
    new->word = newWord;
    new->next = NULL;
    return new;
}

struct list* insert (struct list *theList, string newValue) {
    linkedList *anotherOne = create(newValue);
    anotherOne->next = theList;
    return anotherOne;
}

int hash (string name) {
    return strlen(name);
}

void hashInsert (struct list *theList, string newValue) {
    theList = create(newValue);
    }

int main(void) {
   linkedList *names[24] = {NULL};
   int num = hash("heey");
 //  names[num] = create("heey"); // <- this code works when I uncomment it
   hashInsert(names[num], "heey"); // <-- this causes a segfault.. idk why
   printf("%s", names[num]->word);
}

Your hashInsert function creates a local copy of the pointer ( theList ), you modify said local copy, but the actual pointer in your main function is still set to NULL . 您的hashInsert函数创建指针的本地副本( theList ),您修改了该本地副本,但是main函数中的实际指针仍设置为NULL Calling printf on that is the cause of your segmentation fault. 对此调用printf是导致分段错误的原因。

You can resolve this issue by passing a pointer to the pointer to your function 您可以通过将指针传递给函数的指针来解决此问题

void hashInsert(string list **theList, string newValue) {
    *theList = create(newValue);
}

and then call it 然后叫它

hashInsert(&names[num], "heey");

This way, you modify the value of the pointer from main . 这样,您可以从main修改指针的值。

EDIT 编辑

Also, as the comments state, your malloc does indeed not allocate enough memory, you also need some memory to store the next list pointer. 同样,作为注释状态,您的malloc确实没有分配足够的内存,您还需要一些内存来存储下一个列表指针。

The problem is with you hashInsert function. 问题出在您的hashInsert函数上。 It takes the pointer by value (so the original pointer you pass isn't modified). 它按值获取指针(因此您传递的原始指针不会被修改)。 There's a much better way to go about this- 有更好的方法可以解决这个问题-

struct list* hashInsert(char* string){
    return create(string);
}

A few points aside from that, don't use string , always use char* since that what it really is. 除此之外,请不要使用string ,而应始终使用char*因为这实际上是什么。 I see you're using some library, but you're better off simply including the proper headers yourself, in this case, you should include stdlib.h since it contains the definition of malloc() . 我看到您正在使用某些库,但是最好自己自己包括适当的标头,在这种情况下,您应该包括stdlib.h因为它包含malloc()的定义。

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

相关问题 为什么当我向 main 函数添加任何语句时,我的 C 程序会崩溃? - Why does my C program crash when i add any statement to the main function? 为什么在C和C ++代码中插入URL有效? - Why does inserting URLs in C and C++ code work? 为什么插入printf语句会使我的函数正常工作? - Why does inserting a printf statement make my function work correctly? 为什么我看到太多的C代码忽略了main的返回类型? 为什么行得通? - Why do I see so much C code that omits a return type for main? Why does it work? 为什么我调用 function 时程序会停止? - Why does my program stop when I call a function? 为什么自由函数在我的代码中不起作用? - Why free function does not work in my code? 为什么这个简单的代码有时不起作用????然后当我重新启动我的代码块时它有时会起作用 - why does this simple code gives not work at times????then when i restart my code blocks it does work at times 为什么我的C代码每次都不起作用? - Why does my C code not work everytime? 为什么我的代码块在 main() 中工作而不是在它自己的函数中工作? - Why does my codeblock work in main() but not in its own function? 当main.c不使用pthreads时,为什么必须在main.c编译中显式链接到pthreads? - Why do I have to explicitly link to pthreads in my main.c compilation when my main.c does not use pthreads?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM