简体   繁体   English

对 C 中的指针赋值后的分段错误

[英]Segmentation fault after value assignment to pointer in C

There is code:有代码:

void pop(int id, List *head) {
    List **previous = head;
    List **current = (*previous) -> next;

    if(head -> next == NULL && (*head).id == id) {
        free(head);
    } //if only 1 node in List

    while((**current).id != id) {
        *previous = head -> next;
        *current = previous;
    }

    (*previous) -> next = (*current) -> next;
    free(*current);
}

The program falls with segmentation fault on third stroke List **current = (*previous) -> next (I checked it with printfs).该程序在第三笔划List **current = (*previous) -> next (我用printfs检查过)上出现segmentation fault

CLion debuggers says Exception: EXC_BAD_ACCESS (code=1, address=0x113d349e2) CLion 调试器说Exception: EXC_BAD_ACCESS (code=1, address=0x113d349e2)

If I do printf("address = %d\n", &((*previous) -> next));如果我这样做printf("address = %d\n", &((*previous) -> next)); it gives me the address.它给了我地址。

void delete_worker_by_id(int id, List *head) {
    List **previous = &head;
    List *current = (*previous) -> next;

    if(head -> next == NULL && (*head).id == id) {
        free(head);
        head = NULL;
    } //if only 1 node in List

    while((*current).id != id) {
        *previous = current;
        current = current -> next;
    }

    if((*current).id == id) {
        (*previous) -> next = current -> next;
        current -> next = NULL;
        free(current);
        return;
    }
    puts("No worker with this id");
}

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

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