简体   繁体   English

将结构的指针成员分配为null

[英]Assigning pointer member of a struct to null

I am having some difficulty assigning a pointer inside a struct to null. 我在将结构体内部的指针分配为null时遇到了一些困难。 Since pointers are pass by value, I can't find an easy way to do this. 由于指针是按值传递的,所以我找不到一种简单的方法来执行此操作。 Maybe its been a long day and I can''t think straight. 也许是漫长的一天,我想不通。 Anyways, here is my code: 无论如何,这是我的代码:

void
init_wordrec (wordrec *rec)
{
    if ((rec = (wordrec *) malloc(sizeof(wordrec))) == NULL) {
        perror("Malloc failed");
        exit(1);
    }
    rec->word = NULL;
    rec->hits = 0;
    rec->nxt_wd = NULL;
}

Here is the wordrec struct: 这是wordrec结构:

typedef struct wordrec
{
    char *word;
    int hits;
    struct wordrec *nxt_wd;
} wordrec;

I want the actual word pointer to point to null, unfortunately my attempts have only caused gcc to complain loudly. 我希望实际的单词指针指向null,但不幸的是,我的尝试仅使gcc大声抱怨。

EDIT: Here is a method where I pass in word struct. 编辑:这是我传入单词struct的方法。

void
add_word (char *word, wordrec *head)
{
    wordrec *iter = head;
    wordrec *tmp;
    if (iter->word == NULL) { //This should be NULL but is not
        if ((iter->word = (char *) malloc((strlen(word) + 1) * sizeof(char))) == NULL) {
            perror("Malloc failed");
            exit(1);
        }
        strncpy(iter->word, word, strlen(word) + 1);
        iter->hits++;
        init_wordrec (iter->nxt_wd);
    } else if (strcmp(iter->word, word) < 0) {
        init_wordrec (tmp);
        if ((tmp->word = (char *) malloc((strlen(word) + 1) * sizeof(char))) == NULL) {
            perror("Malloc failed");
            exit(1);
        }
        strncpy(tmp->word, word, strlen(word) + 1);
        tmp->hits++;
        tmp->nxt_wd = head; 
        head = tmp;
    } else if (strcmp(iter->word, word) > 0) {
        add_word (word, iter->nxt_wd);
    } else {
        iter->hits++;
    }
}

Main: 主要:

int 
main()
{
    wordrec head;
    char word1[] = "Hello";
    init_wordrec (&head);
    add_word(word1, &head);
    return 0;
}

Did you mean something like this: 您的意思是这样的吗:

void init_wordrec(wordrec **rec)
{
    if ((*rec = (wordrec *) malloc(sizeof(wordrec))) == NULL) {
        perror("Malloc failed");
        exit(1);
    }
    (*rec)->word = NULL;
    (*rec)->hits = 0;
    (*rec)->nxt_wd = NULL;
}
....
wordrec *wr;
init_wordrec(&wr);

Assignment goes the other way — you assign a value, such as NULL , to a variable, such as rec->word . 分配是另一种方式-您将一个值(例如NULL分配给一个变量(例如rec->word

You could pass a double pointer to the function: 您可以将双指针传递给该函数:

void
init_wordrec (wordrec **rec)
{
    if ((*rec = (wordrec *) malloc(sizeof(wordrec))) == NULL) {
        perror("Malloc failed");
        exit(1);
    }
    (*rec)->word = NULL;
    (*rec)->hits = 0;
    (*rec)->nxt_wd = NULL;
}
/* ... */
wordrec *wr = 0;
init_wordrec(&wr);

but since you're allocating in the function (so you're just going to throw away the value passed in) making it return the new record usually simplifies the code: 但是由于您要分配该函数(因此,您将只丢弃传入的值),因此使其返回新记录通常会简化代码:

wordrec*
init_wordrec(void)
{
    wordrec *rec = malloc(sizeof(*rec));
    if (rec == NULL) {
        perror("Malloc failed");
        exit(1);
    }
    rec->word = NULL;
    rec->hits = 0;
    rec->nxt_wd = NULL;
    return rec;
}
/* ... */
wordrec *wr = init_wordrec();

You shouldn't cast the result of malloc ; 您不应该转换malloc的结果; it will make the code compile even if you fail to include its prototype, but will fail badly at runtime. 即使您没有包括它的原型,它也会使代码编译,但是在运行时会严重失败。

A simpler option would be: 一个更简单的选择是:

void init_wordrec (wordrec *rec)
{
    rec->word = NULL;
    rec->hits = 0;
    rec->nxt_wd = NULL;
}

which allows your code in main to remain unchanged: 这可以使您的main代码保持不变:

int main()
{
    wordrec head;
    char word1[] = "Hello";
    init_wordrec (&head);

Another advantage of this is that users of wordrec have the option to use automatic allocation or dynamic allocation (whereas the other proposed answers force dynamic allocation to be used). 这样做的另一个优点是wordrec用户可以选择使用自动分配还是动态分配(而其他建议的答案则强制使用动态分配)。

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

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