我有以下代码(这里简化).
#include <stdio.h>
#include <stdlib.h>
struct inner {
int a;
int b;
};
struct outer {
struct inner *next;
struct inner *prev;
};
int main(int argc, char *argv[]) {
struct outer *c = malloc(sizeof(struct outer));
if (c->next) {
printf("here\n");
}
if (c->next != NULL) {
printf("here too");
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,c
仅分配了内存,因此c->next
并且c->prev
是NULL地址.但两份印刷报表仍然有效.if
如果c->next
不是空指针,我如何使语句工作?
A. malloc()
没有将内存初始化为零,所以你的指针不一定NULL
B.访问未初始化的指针会导致未定义的行为
C. c->next
和c->next != NULL
基本相同的条件检查
归档时间: |
|
查看次数: |
344 次 |
最近记录: |