不执行if语句

use*_*818 0 c pointers if-statement

我有一个辅助函数.

enum recstate {
    initial,
};

struct client {
    enum recstate state;
}
Run Code Online (Sandbox Code Playgroud)

这是我的主要代码:

struct client *p;
p->state = initial;
Run Code Online (Sandbox Code Playgroud)

一切正常,直到我尝试执行以下if语句:

if(p->state == initial){
Run Code Online (Sandbox Code Playgroud)

我真的不明白为什么.非常感谢.谢谢!

R S*_*ahu 7

你还没有分配内存p.您正在访问未经授权的内存 未定义访问未授权内存的行为.

p = malloc(sizeof(struct client)); 
Run Code Online (Sandbox Code Playgroud)

在线之前

p->state = initial;
Run Code Online (Sandbox Code Playgroud)