c - 代码中的奇怪错误

标签 c compiler-errors compilation syntax-error

我的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char *str, c;
    int x = 0, y = 1;

    str = (char*)malloc(sizeof(char));

    printf("Inserisci stringa principale : ");

        while (c != '\n') {
        // read the input from keyboard standard input
        c = getc(stdin);

        // re-allocate (resize) memory for character read to be stored
        str = (char*)realloc(str, y * sizeof(char));

        // store read character by making pointer point to c
        str[x] = c;

        x++;
        y++;
        }

    str[x] = '\0'; // at the end append null character to mark end of string

    printf("\nLa stringa inserita : %s", str);

      char *sub, b;
      int w = 0, z = 1;

      sub = (char*)malloc(sizeof(char));

      printf("Immetti sottostringa da cercare : ");

          while (b != '\n') {
            // read the input from keyboard standard input
            b = getc(stdin);

            // re-allocate (resize) memory for character read to be stored
            sub = (char*)realloc(sub, z * sizeof(char));

            // store read character by making pointer point to c
            sub[w] = b;

            w++;
            z++;
          }

      sub[w] = '\0'; // at the end append null character to mark end of string

    char *p1, *p2, *p3;
    int i=0,j=0,flag=0, occurrences=0;

      p1 = str;
      p2 = sub;

      for(i = 0+1; i<strlen(str); i++)
      {
        if(*p1 == *p2)
          {
              p3 = p1;


              for(j = 0;j<strlen(sub);j++)
              {
                if(*p3 == *p2)
                {
                  p3++;p2++;
                } 
                else
                  break;
              }
              p2 = sub;
              if(j + 1 == strlen(sub))
              {
                 flag = 1;
                 occurrences = occurrences + 1;
                printf("\nnel numero di volte : %d\n",occurrences );
                printf("\nSottostringa trovata all'indice : %d\n",i );
              }

          }
        p1++; 
      }


      if(flag==0)
      {
           printf("Sottostringa non trovata");
      }
    free(str);
    free(sub);
    return (0);
    }

当我尝试编译时收到此错误

> myfile.h:1: error: stray '\239' in program
> myfile.h:1: error: stray '\187' in program
> myfile.h:1: error: stray '\191' in program

我试图找到一个解决方案,但我完全不知道这是什么,这个错误

我尝试编译了很多次,每次我都收到这个错误,我不知道为什么

最佳答案

字节序列239187191UTF-8 Byte Order Mark (BOM) .

您没有使用纯文本编辑器,或者要求您的编辑器使用带有 BOM 的 UTF-8 进行保存。不要那样做。另存为纯文本

关于c - 代码中的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44626169/

相关文章:

java - 用户定义方法java的问题

c - make—我可以抑制格式截断错误吗?

java - 向调用方法抛出错误!

c - 如何可靠地将字符数组转置为 uint64_t 数组并再次转回

node.js - 当我尝试将代码作为字符串发送到jdoodle编译器api时出现错误

C++ VS2010 编译选项

c - 在 Linux 中使用 makefile 将源文件更新到不同的文件夹

python - Python 内置 'compile' 的主要用途是什么?

C 分段故障函数引用

c - 我应该如何将变量传递给 system() C 调用?