C语言函数指针为空判断 - 完整示例与解析
"在C语言中,可以使用一个函数指针的值来判断它是否为空。如果一个函数指针为空,说明它没有被赋值为一个有效的函数地址。\n\n以下是一个示例代码:\n\nc\n#include <stdio.h>\n\nvoid func() {\n printf("This is a function.\n");\n}\n\nint main() {\n void (*ptr)() = NULL;\n\n if (ptr == NULL) {\n printf("Function pointer is NULL.\n");\n } else {\n printf("Function pointer is not NULL.\n");\n }\n\n ptr = func;\n\n if (ptr == NULL) {\n printf("Function pointer is NULL.\n");\n } else {\n printf("Function pointer is not NULL.\n");\n }\n\n return 0;\n}\n\n\n在上面的示例中,我们首先将函数指针ptr初始化为空(即NULL)。然后,我们使用条件语句检查ptr是否为空。由于ptr的值是NULL,因此条件为真,我们打印出"Function pointer is NULL.". 接下来,我们将ptr赋值为一个有效的函数地址func,然后再次检查ptr是否为空。由于ptr的值不再是NULL,因此条件为假,我们打印出"Function pointer is not NULL.".\n\n总之,在C语言中,我们可以使用NULL来表示一个空的函数指针,并使用条件语句来判断函数指针是否为空。\n\n"
原文地址: http://www.cveoy.top/t/topic/pVXc 著作权归作者所有。请勿转载和采集!