简体   繁体   English

在C中将指针设置为NULL,而不进行编译

[英]Setting a pointer to NULL in C, not compiling

for some reason, when I try to compile my code the .exe file just disappears after its done... without any errors or warnings. 由于某种原因,当我尝试编译我的代码时,.exe文件在完成后便消失了……而没有任何错误或警告。

This is what my code looks like: 这是我的代码如下所示:

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

typedef struct student_ 
{
    int matnum;
    char vorname[20];
    struct student_ *next;
} student;

int main (int argc, char **argv) 
{
    student hans;
    hans.matnum = 12;

    student peter;
    peter.matnum = 13;
    peter.next = NULL; // THIS PART CAUSES MY PROBLEM

    hans.next = &peter;

    student *curr = &hans;
    while(curr != NULL)
    {
        printf("matnum: %d\n", (*curr).matnum);
        curr = curr->next;
    }

    return 0; 
}

What I want it to do is iterate through curr and set it to the next everytime curr wasn't NULL already. 我想要它做的是遍历curr,并将其设置为每次curr尚未为NULL时的下一个。 So if next is NULL, than the while-loop should stop. 因此,如果next为NULL,则while循环应停止。

peter.next = NULL; // THIS PART CAUSES MY PROBLEM

This is how I want to achieve that but it doesn't work. 这是我想要实现的方法,但是没有用。 :\\ :\\


From the comments: 从评论:

I don't get an error. 我没有错。 AFter I type in "gcc -Wall -o test test.c" the test.exe is there for a second and then it gets deleted. 在我输入“ gcc -Wall -o test test.c”之后,test.exe会出现一秒钟,然后将其删除。

The most plausible explanation for this is that your anti-virus software is deleting the executable. 对此的最合理的解释是您的防病毒软件正在删除可执行文件。 Your program compiles and runs fine here. 您的程序在这里编译并运行良好。

For whatever reason, the suspect line results in compiled code that your anti-virus software matches against a known virus. 无论出于何种原因,可疑程序行都会生成防病毒软件与已知病毒匹配的已编译代码。 When you remove that line, it no longer matches the virus, but of course the program fails. 当您删除该行时,它不再与病毒匹配,但是该程序当然会失败。

Temporarily disable your anti-virus to confirm this hypothesis. 暂时禁用您的防病毒软件以确认此假设。

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

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