C指针分段错误

C指针分段错误,c,pointers,segmentation-fault,C,Pointers,Segmentation Fault,我有一个奇怪的分割错误。我正在尝试查找使用指针的患者列表中是否已经存在患者id。我认为有关守则是: #include <stdio.h> #include <stdlib.h> #include "health.h" void addPatient(int patientID) { printf("here1"); Chart* patients_chart; // find the patient with id patients_char

我有一个奇怪的分割错误。我正在尝试查找使用指针的患者列表中是否已经存在患者id。我认为有关守则是:

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

void addPatient(int patientID) {
printf("here1"); 
    Chart* patients_chart;

    // find the patient with id
    patients_chart = patientList;

    while(patients_chart == NULL || patients_chart->id == patientID) {
        patients_chart = patients_chart->next;
printf("here2");
    }

printf("here3");

    // if patient wasn't found, add new patient
    if (patients_chart == NULL) {
        Chart *new_chart;
printf("here4");
        // allocate and initialize new patient
        new_chart         = (Chart*)malloc(sizeof(Chart));
        new_chart->id     = patientID;
        new_chart->buffer = NULL;

        // insert new patient into list
        new_chart->next   = patientList;
        patientList       = new_chart;
printf("here5");
    }
}
我在main中使用如下输入调用函数:1,12:12:12,7,0

7是“命令”

1是有问题的患者id

你可以忽略其余的


我知道如何找到病人,但我遇到了这个恼人的seg故障。谢谢你抽出时间

以下代码有缺陷:

while(patients_chart == NULL || patients_chart->id == patientID) {
    patients_chart = patients_chart->next;
    printf("here2");
}
只要指针为
NULL
或指针与患者ID匹配,您就可以继续前进。您缺少一个否定。相反,请使用:

while(patients_chart != NULL && patients_chart->id != patientID) {
    patients_chart = patients_chart->next;
    printf("here2");
}

以下代码有缺陷:

while(patients_chart == NULL || patients_chart->id == patientID) {
    patients_chart = patients_chart->next;
    printf("here2");
}
只要指针为
NULL
或指针与患者ID匹配,您就可以继续前进。您缺少一个否定。相反,请使用:

while(patients_chart != NULL && patients_chart->id != patientID) {
    patients_chart = patients_chart->next;
    printf("here2");
}

以下代码有缺陷:

while(patients_chart == NULL || patients_chart->id == patientID) {
    patients_chart = patients_chart->next;
    printf("here2");
}
只要指针为
NULL
或指针与患者ID匹配,您就可以继续前进。您缺少一个否定。相反,请使用:

while(patients_chart != NULL && patients_chart->id != patientID) {
    patients_chart = patients_chart->next;
    printf("here2");
}

以下代码有缺陷:

while(patients_chart == NULL || patients_chart->id == patientID) {
    patients_chart = patients_chart->next;
    printf("here2");
}
只要指针为
NULL
或指针与患者ID匹配,您就可以继续前进。您缺少一个否定。相反,请使用:

while(patients_chart != NULL && patients_chart->id != patientID) {
    patients_chart = patients_chart->next;
    printf("here2");
}
这里,如果条件1(
patients\u chart==NULL
)为真,则执行以下操作:
患者图表=患者图表->下一步

这是空指针解引用,因此导致Seg故障

这里,如果条件1(
patients\u chart==NULL
)为真,则执行以下操作:
患者图表=患者图表->下一步

这是空指针解引用,因此导致Seg故障

这里,如果条件1(
patients\u chart==NULL
)为真,则执行以下操作:
患者图表=患者图表->下一步

这是空指针解引用,因此导致Seg故障

这里,如果条件1(
patients\u chart==NULL
)为真,则执行以下操作:
患者图表=患者图表->下一步



这是空指针解引用,从而导致Seg故障。

您在哪一点有Seg故障?您的搜索循环条件似乎不好。@unwind非常感谢!你刚刚证明了我的教授在某种程度上是错的。在哪一点上你有错?您的搜索循环条件似乎不好。@unwind非常感谢!你刚刚证明了我的教授在某种程度上是错的。在哪一点上你有错?您的搜索循环条件似乎不好。@unwind非常感谢!你刚刚证明了我的教授在某种程度上是错的。在哪一点上你有错?您的搜索循环条件似乎不好。@unwind非常感谢!你只是在某种程度上证明了我的教授是错的。我试图证明它并没有解决问题。不过,我的循环逻辑似乎不正确。这解决了我的问题。但归根结底是指针解引用,我试图证明它并没有解决问题。不过,我的循环逻辑似乎不正确。这解决了我的问题。但归根结底是指针解引用,我试图证明它并没有解决问题。不过,我的循环逻辑似乎不正确。这解决了我的问题。但归根结底是指针解引用,我试图证明它并没有解决问题。不过,我的循环逻辑似乎不正确。这解决了我的问题。但归根结底是指针解引用。就是这样!患者图表=患者列表;如果(patients_chart!=NULL){而(patients_chart->id!=patientID){patients_chart=patients_chart->next;}}就是这样!患者图表=患者列表;如果(patients_chart!=NULL){而(patients_chart->id!=patientID){patients_chart=patients_chart->next;}}就是这样!患者图表=患者列表;如果(patients_chart!=NULL){而(patients_chart->id!=patientID){patients_chart=patients_chart->next;}}就是这样!患者图表=患者列表;if(patients_chart!=NULL){而(patients_chart->id!=patientID){patients_chart=patients_chart->next;}