Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C程序';s函数似乎总是返回true,但我不&x27;我不明白为什么_C_Arrays_If Statement_Boolean - Fatal编程技术网

C程序';s函数似乎总是返回true,但我不&x27;我不明白为什么

C程序';s函数似乎总是返回true,但我不&x27;我不明白为什么,c,arrays,if-statement,boolean,C,Arrays,If Statement,Boolean,(我是Stackoverflow的新手。) 我目前在C的正则表达式引擎上工作,但是有一些问题 第一个函数称为“contains”。如果将两个字符数组传递给该func,一个是源,另一个是在该文本中查找的指针,则如果文本包含代码段,则返回true;如果文本不包含代码段,则返回false(同样,如果指针数组作为文本数组具有更多元素) 我使用gcc编译: gcc -std=c99 test.c regex.c -o test /测试 它总是打印“包含”,即使针的长度更大 片段: bool contai

(我是Stackoverflow的新手。)

我目前在C的正则表达式引擎上工作,但是有一些问题

第一个函数称为“contains”。如果将两个字符数组传递给该func,一个是源,另一个是在该文本中查找的指针,则如果文本包含代码段,则返回true;如果文本不包含代码段,则返回false(同样,如果指针数组作为文本数组具有更多元素)

我使用gcc编译:

gcc -std=c99 test.c regex.c -o test
/测试

它总是打印“包含”,即使针的长度更大

片段:

bool contains(const char source[], const char needle[]){

unsigned int source_length = (int)sizeof(source)/sizeof(source[0]);
unsigned int needle_length = (int)sizeof(needle)/sizeof(needle[0]);

if(source_length < needle_length) return false; // this should return false to the test program but it doesn't. What's wrong?

}
bool包含(常量字符源[],常量字符指针[]){
无符号int-source_-length=(int)sizeof(source)/sizeof(source[0]);
无符号整数针长度=(int)sizeof(针)/sizeof(针[0]);
if(source_length
我想我只是不知道怎么了。有人能帮我吗

多谢各位


尼克

T[]
作为函数参数与
T*
相同。在您的示例中,
source
needle
都是
const char*
,因此两者的大小相同。因此,测试总是失败。您可以通过打印出
sizeof(源)
sizeof(针)
来轻松检查这一点

如果指针指向以nul结尾的字符串,则可以使用
strlen
获取其长度。否则,您需要传递不同的函数参数


另外,请注意,在所有情况下都需要返回一些内容。当测试失败时,您不会返回。

T T[]
作为函数参数与
T*
相同。在您的示例中,
source
needle
都是
const char*
,因此两者的大小相同。因此,测试总是失败。您可以通过打印出
sizeof(源)
sizeof(针)
来轻松检查这一点

如果指针指向以nul结尾的字符串,则可以使用
strlen
获取其长度。否则,您需要传递不同的函数参数


另外,请注意,在所有情况下都需要返回一些内容。当测试失败时,您不会返回。

您正在将指向字符的指针的大小除以单个字符的大小。它们都是常量长度,因此它总是返回true

具体来说,在我工作过的操作系统上

sizeof(source) // size of const char* == size of pointer to char == 4
sizeof(needle) // size of const char* == size of pointer to char == 4
sizeof(source[0]) // size of char == 1 
sizeof(neeedle[0]) // size of char == 1 

将指向一个字符的指针的大小除以单个字符的大小。它们都是常量长度,因此它总是返回true

具体来说,在我工作过的操作系统上

sizeof(source) // size of const char* == size of pointer to char == 4
sizeof(needle) // size of const char* == size of pointer to char == 4
sizeof(source[0]) // size of char == 1 
sizeof(neeedle[0]) // size of char == 1 

此函数声明

bool contains(const char source[], const char needle[]);
相当于

bool contains(const char *source, const char *needle);
unsigned int source_length = (int)sizeof( const char * )/sizeof( const char );
unsigned int needle_length = (int)sizeof( const char * )/sizeof( const char );
所以在函数表达式中

unsigned int source_length = (int)sizeof(source)/sizeof(source[0]);
unsigned int needle_length = (int)sizeof(needle)/sizeof(needle[0]);
相当于

bool contains(const char *source, const char *needle);
unsigned int source_length = (int)sizeof( const char * )/sizeof( const char );
unsigned int needle_length = (int)sizeof( const char * )/sizeof( const char );
两者都等于
sizeof(const char*)
,因为
sizeof(const char)
等于1

您必须使用标准的C函数来比较字符串的长度

比如说

bool contains(const char source[], const char needle[])
{
   return ( !( strlen( source ) < strlen( needle ) ) );
}
bool包含(常量字符源[],常量字符指针[])
{
返回(!(strlen(源)
此函数声明

bool contains(const char source[], const char needle[]);
相当于

bool contains(const char *source, const char *needle);
unsigned int source_length = (int)sizeof( const char * )/sizeof( const char );
unsigned int needle_length = (int)sizeof( const char * )/sizeof( const char );
所以在函数表达式中

unsigned int source_length = (int)sizeof(source)/sizeof(source[0]);
unsigned int needle_length = (int)sizeof(needle)/sizeof(needle[0]);
相当于

bool contains(const char *source, const char *needle);
unsigned int source_length = (int)sizeof( const char * )/sizeof( const char );
unsigned int needle_length = (int)sizeof( const char * )/sizeof( const char );
两者都等于
sizeof(const char*)
,因为
sizeof(const char)
等于1

您必须使用标准的C函数来比较字符串的长度

比如说

bool contains(const char source[], const char needle[])
{
   return ( !( strlen( source ) < strlen( needle ) ) );
}
bool包含(常量字符源[],常量字符指针[])
{
返回(!(strlen(源)
在C语言中,数组是通过引用而不是值传递给函数的。这意味着函数只看到指向第一个元素的指针,不知道最初分配了多少空间。您必须手动计算元素的数量——通过迭代每个元素直到获得空字符

您可以使用辅助函数,如
strlen
,但如果不希望导入其他库,请尝试以下代码:

bool contains(const char source[], const char needle[])
{
  unsigned int source_length = 0;
  unsigned int needle_length = 0;
  while(source[source_length++] != '\0');
  while(needle[needle_length++] != '\0');
  return !(source_length < needle_length);
}
bool包含(常量字符源[],常量字符指针[])
{
无符号整数源_长度=0;
无符号整数针长度=0;
while(source[source_length++]!='\0');
而(针[针的长度++!='\0');
返回!(源长度<针长度);
}

请注意,如果字符串是在
contains()
中分配的,而不是作为参数传递给函数,那么您的解决方案就会起作用。

在C中,数组是通过引用而不是值传递给函数的。这意味着函数只看到指向第一个元素的指针,不知道最初分配了多少空间。您必须手动计算元素的数量——通过迭代每个元素直到获得空字符

您可以使用辅助函数,如
strlen
,但如果不希望导入其他库,请尝试以下代码:

bool contains(const char source[], const char needle[])
{
  unsigned int source_length = 0;
  unsigned int needle_length = 0;
  while(source[source_length++] != '\0');
  while(needle[needle_length++] != '\0');
  return !(source_length < needle_length);
}
bool包含(常量字符源[],常量字符指针[])
{
无符号整数源_长度=0;
无符号整数针长度=0;
while(source[source_length++]!='\0');
而(针[针的长度++!='\0');
返回!(源长度<针长度);
}

请注意,如果字符串是在
contains()
中分配的,而不是作为参数传递给函数,那么您的解决方案就会起作用。

这是如何编译的?并非所有代码路径都返回值。sizeof不提供数组中的元素数。它返回指针的大小,可以是4或8。您需要
strlen
在运行时获取字符串的长度,而不是
sizeof
@Beed。在许多情况下,这是获取数组中项目数的有效习惯用法。但它只在