简体   繁体   English

C-有没有办法在char数组中首先区分NULL和'\\ 0'?

[英]C - is there a way to distinguish between NULL and '\0' as first in a char array?

I am having a issue where I want to return NULL if there are no "matches" in two strings versus '\\0' if the string was empty to begin with. 我遇到一个问题,如果两个字符串中没有“ matches”,而如果字符串开头为空,则我想返回NULL。 Is there a way to distinguish between the two? 有没有办法区分两者?

In my case I have a function returning a char*. 就我而言,我有一个返回char *的函数。 What are my options to have a distinction between the two so I can have two different print statements depending on if its NULL (meaning the character array wasn't null but returned null for conditional reasons in my logic) or '\\0' (meaning the character array that was passed into the function was just the empty string ""). 有什么区别可以区分两者,因此我可以有两个不同的打印语句,具体取决于它的NULL(表示字符数组不是null而是由于逻辑上的条件性原因返回null)还是'\\ 0'(表示传递给函数的字符数组只是空字符串“”)。

Thanks 谢谢

if (ptr == NULL) {
    // Stuff to do for NULL case
} elseif (*ptr == 0) {
    // Stuff to do for empty string
} else {
    // Stuff to do for other cases
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM