什么事!s | | |*什么意思?(s是char*)
我正在努力理解这部分的含义:什么事!s | | |*什么意思?(s是char*),c,string,C,String,我正在努力理解这部分的含义: if (!s || !*s) //What is this?!?! { printf("\n"); return; } 以下是我的主要功能: #include <conio.h> #include <stdio.h> #include <string.h> void func1(char *s); int main() { func1("SABABA");
if (!s || !*s) //What is this?!?!
{
printf("\n");
return;
}
以下是我的主要功能:
#include <conio.h>
#include <stdio.h>
#include <string.h>
void func1(char *s);
int main()
{
func1("SABABA");
return 0;
}
#包括
#包括
#包括
void func1(字符*s);
int main()
{
职能1(“萨巴巴”);
返回0;
}
我的职责1:
void func1(char *s)
{
int a, b, c;
if (!s || !*s)
{
printf("\n");
return;
}
b = strlen(s);
while (b>2)
{
a = 0;
if (s[a] == s[b - 1])
{
for (c = b - 1; c >= a && s[a] == s[c]; a++, c--);
if (c<a)
{
int i;
for (i = 0; i<b; i++)
printf("%c", s[i]);
printf(" ");
}
}
b--;
}
func1(s + 1);
}
void func1(char*s)
{
INTA、b、c;
如果(!s | |!*s)
{
printf(“\n”);
返回;
}
b=strlen(s);
而(b>2)
{
a=0;
如果(s[a]==s[b-1])
{
对于(c=b-1;c>=a&s[a]==s[c];a++,c--);
如果(c!s
正在检查s
不是空指针
!*s
正在检查指向的字符s
不是'\0'
,这意味着s
不是空字符串
该检查与调用的func1(“SABABA”)
不匹配,但准备用于func1(0)
或func1(“”)
语句if(!s | |!*s)
检查指针s
是否为空指针,或者指针的目标是否为'\0'
。需要进行前一项检查,因为您可以为该函数提供来自malloc
的指针。以下是一个示例:
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void func1(char *s);
int main()
{
char *str = "SABABA";
char *ptr = (char*)malloc(10 * sizeof(char));
func1(str);
//ptr may have null value,that's why we check in func1
func1(ptr);
free(ptr);
return 0;
}
#包括
#包括
#包括
#包括
void func1(字符*s);
int main()
{
char*str=“SABABA”;
char*ptr=(char*)malloc(10*sizeof(char));
功能1(str);
//ptr可能有空值,这就是我们签入func1的原因
职能1(ptr);
免费(ptr);
返回0;
}
阅读一本关于C的介绍性书籍中的指针。!s
检查s
是否为空指针,!*s
取消引用s
并检查它是否指向NUL字符。这些都是糟糕的变量和函数名!:D@mikeyq6它是从一个测试中得到的。它只有7个点,用来确定这个函数是什么关于DOS和它的输出。我想他们是在1997年写的。为什么!s是必要的?它不是动态分配。它的意思是“萨巴巴”将被分配给s,这显然会有内存。或者,如果RAM已满,它将返回一个空指针?@IlanAizelmanWS:为什么你认为它不应该返回空指针?假设s
为空,如果你试图遵从它,会发生什么?仅仅因为你专门用非空字符串调用函数,并不意味着它总是会返回空指针,而且你不应该假设它会这样。@IlanAizelmanWSfunc1()
不知道它将如何调用,以及将传递什么,不是吗?想想func1(0)
和func1(“”)
@IlanAizelmanWS可能没有必要检查!s
。这取决于为函数编写规范的人是否希望它处理空指针。该规范可以同样轻松地禁止向函数传递空指针值,在这种情况下,程序员不需要为其编写代码特别是(但可能会决定不考虑此问题)。“ptr可能有空值,这就是为什么我们签入func1”不正确。func1
将只检查*ptr
,不包括其他9个字符。