Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 字符串比较差异(适用于某些情况,有时不适用)_C_Arrays_String_Pointers_Strcmp - Fatal编程技术网

C 字符串比较差异(适用于某些情况,有时不适用)

C 字符串比较差异(适用于某些情况,有时不适用),c,arrays,string,pointers,strcmp,C,Arrays,String,Pointers,Strcmp,我使用以下代码测试了我收到的电子邮件文件中的一些文本检测 #include <stdio.h> int main() { int max_size = 100; char email[100] ="aheanstring4strinstringzil.comg"; int pointer = 0; int i = 1; int j = 0; int len = 0; int chk = 0; char found1[7] = "string1";

我使用以下代码测试了我收到的电子邮件文件中的一些文本检测

#include <stdio.h>

int main()
{
  int max_size = 100;
  char email[100] ="aheanstring4strinstringzil.comg";
  int pointer = 0;
  int i = 1;
  int j = 0;
  int len = 0;
  int chk = 0;
  char found1[7] = "string1";
  char found2[7] = "string2";
  char found3[7] = "string3";
  char found4[7] = "string4";
  char found5[7] = "string5";

  char user1[7] = "string1"; // 23        
  char user2[7] = "string2";// 19        
  char user3[7] = "string3"; // 14        
  char user4[7] = "string4"; // 16      
  char user5[7] = "string5";; // 15

  while (pointer != max_size && chk != 1)
  {

    for (j = 0;j<7; j++)
    {
      found1[j] = *(email+pointer+j);
    }

    if (strcmp(found1, user1) == 0){
      printf("Authorized User Found\n");
      chk = 1;
      continue;
    }

    for (j = 0;j<7; j++)
    {
      found2[j] = *(email+pointer+j);
    }

    if (strcmp(found2, user2) == 0){
      printf("Authorized User Found\n");
      chk = 1;
      continue;
    }

    for (j = 0;j<7; j++)
    {
      found3[j] = *(email+pointer+j);
    }

    if (strcmp(found3, user3) == 0){
      printf("Authorized User Found\n");
      chk = 1;
      continue;
    }

    for (j = 0;j<7; j++)
    {
      found4[j] = *(email+pointer+j);
    }

    if (strcmp(found4, user4) == 0){
      printf("Authorized User Found\n");
      chk = 1;
      continue;
    }


    for (j = 0;j<7; j++)
    {
      found5[j] = *(email+pointer+j);
    }

    if (strcmp(found5, user5) == 0){
      printf("Authorized User Found\n");
      chk = 1;
      continue;
    }

    pointer++;

  }
  printf("Check is %d, Pointer is %d\n",chk, pointer);
  return 0;
}
#包括
int main()
{
int max_size=100;
char email[100]=“aheanstring4strinstringzil.com”;
int指针=0;
int i=1;
int j=0;
int len=0;
int-chk=0;
char found1[7]=“string1”;
char found2[7]=“string2”;
char found3[7]=“string3”;
char found4[7]=“string4”;
char found5[7]=“string5”;
char user1[7]=“string1”//23
char user2[7]=“string2”//19
char user3[7]=“string3”;//14
char user4[7]=“string4”;//16
char user5[7]=“string5”;//15
while(指针!=max_size&&chk!=1)
{
对于(j=0;j
此处
found1
在C中不是有效字符串,因为没有nul终止符。您需要

char found1[8] = "string1";
您将
found1
传递给
strcmp()
,这将导致未定义的行为,因为
strcmp()
需要以null结尾的字符串

或者正如巴拉克·马诺斯所建议的那样

char found1[] = "string1";

[7]
更改为
[]
。检查编译器警告(如果没有看到任何编译器警告,请检查编译器警告设置)。
警告:函数“strcmp”的隐式声明
:未包含
字符串.h
。即“它工作正常”这只是运气——它可能对其他功能不起作用。非常感谢。它起作用了。@Mohamed,如果我回答了你的问号,答案是一样的
char found1[] = "string1";