我在写这个程序来练习C语言编程,这是我的代码:C编程:铸造“无效指针”指向结构
#include <stdio.h>
#include <stdlib.h>
struct s1 {
int i;
void * p;
};
static struct s1 *dmk;
int main(void) {
int tong(int a, int b);
int (*tinh)(int,int);
struct s2 {
int num;
int (*cal)(int a, int b);
};
if(dmk->p == NULL)
{
printf("NULL ALERT\n");
}
struct s2 *cl = dmk->p;
cl->cal = tong;
tinh = ((struct s2 *)(dmk->p))->cal;
printf("tinh 2, 4 ra %d\n",tinh(2,4));
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}
int tong(int a, int b)
{
return a + b;
}
当我编译它,它没有表现出任何错误或警告。但是当我运行程序时,终端告诉我“核心转储”并没有显示任何结果接受“NULL ALERT”。谁能解释我为什么失败?非常感谢。
在我引用的陈述中用' - >'调用UB是个问题。编码器方面出现“*失败*”。 @ T.J.Crowder – alk
实际上'dmk' **被**初始化,因为它是一个全局变量。问题:它是一个_null pointer_。 – Olaf
@Olaf:正确,修正,谢谢。 – alk