首页 > 解决方案 > 如何检查字符串是否已定义?

问题描述

我有如下代码。
我想检查一个字符串是否已定义或不使用isdefined函数。
我该怎么做?

#include <stdio.h>
#include <stdbool.h>
#define string char*

bool isdefined(string str){
     /*do something*/
}

int main(){
    string hw;                 //print NO if I write this line
    string hw = "Hello World"; //print YES if I write this line
    if(isdefined(hw))
        printf("YES");
    else
        printf("NO");
    return 0;
}

标签: cstring

解决方案


这是不可能的。

您所能做的就是检查它是否为,但不能NULL保证将未初始化的自动变量设置为任何特定值,因此它不适用于您显示的代码。

“定义”在这里也不是正确的术语,它在 C 中意味着其他东西。


推荐阅读