c - 段错误 :I can't understand why

标签 c segmentation-fault

当我尝试在两个不同的地方使用相同的函数时,出现了一个非常奇怪的段错误错误。

printTVNode 函数在 main 上运行良好。 在主要部分:

printTVNode(headTVNode); /* Works fine here */

TVNodePointer headTopic = NULL;
TopicEmmissions(&headTopic,headTVNode,desiredTopic);

当我尝试在 TopicEmmissions 函数中使用 printTVNode 时,会出现 Seg 错误。

void TopicEmmissions(TVNodePointer * headTopic,TVNodePointer headTVNode,char * desiredTopic){
    TVNodePointer currentTVNode = headTVNode;
    EmmissionPointer currentEmmission;
    EventPointer currentEvent;
    EventPointer topicSubjects = NULL;
    int flag,countEvent = 1,countEmmission = 1;

    printTVNode(headTVNode); /* Get Segmentation Fault here*/
    ...

printTVNode函数:

void printTVNode(TVNodePointer headTVNode){
    TVNodePointer currentTVNode = headTVNode;

    while ( currentTVNode != NULL ){
        printEmmission(*(currentTVNode->anEmmission));

        currentTVNode = currentTVNode->next;
    }
}

最佳答案

问题似乎出在以下行:

printEmmission(*(currentTVNode->anEmmission));

anEmmissionNULL 的情况下,当您尝试取消引用它时,我认为您会遇到段错误。

在取消引用之前,请确保检查 anEmmission 不为 NULL

关于c - 段错误 :I can't understand why,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29853548/

相关文章:

c - 定义指向内存地址的指针并打印内容 C++

c - 分段故障;初学者

c - 段错误: Reading values from a text file

c++ - 段错误: 11 c++ when using vector

复制链表C中的节点

c++ - 函数指针类型不能用于函数原型(prototype)

C 项目欧拉数素数

捕获意外信号 : SIGSEGV (11). 无效内存访问

c - 几次重新分配后出现段错误 |结构数组

c - 为什么错误 "Segmentation fault"在这里?