#include <stdio.h>
#include <string.h>
char *bf_match(char *pat , char *txt){
char *pt; /* txt をなぞるカーソル */
char *pp; /* pat をなぞるカーソル */
int txt_len = strlen(txt); /* txt の文字数 */
int pat_len = strlen(pat); /* pat の文字数 */
pt = txt;
pp = pat;
while(*pt != '\0' && *pp != '\0'){
if(*pt == *pp){
pt++;
pp++;
}else{
pt= pt - pp + 1;
pp = 0;
}
}
if(*pp == '\0'){
return pt - pp;
}
return NULL;
}
int main(void){
char *s;
char s1[80]; /* テキスト */
char s2[80]; /* パターン */
printf("テキスト:");
scanf("%s", s1);
printf("パターン:");
scanf("%s", s2);
s = bf_match(s2, s1);
if (s == NULL)
puts("テキスト中にパターンは存在しません。");
else
printf("%d 文字目に見つかりました。\n", s+1);
return 0;
}
Build Errorは以下が表示されました。
Main.c:22:12: warning: incompatible integer to pointer conversion assigning to 'char *' from 'long' [-Wint-conversion]
pt= pt - pp + 1;
^ ~~~~~~~~~~~
Main.c:28:13: warning: incompatible integer to pointer conversion returning 'long' from a function with result type 'char *' [-Wint-conversion]
return pt - pp;
^~~~~~~
Main.c:8:6: warning: unused variable 'txt_len' [-Wunused-variable]
int txt_len = strlen(txt); /* txt の文字数 */
^
Main.c:9:6: warning: unused variable 'pat_len' [-Wunused-variable]
int pat_len = strlen(pat); /* pat の文字数 */
^
Main.c:10:6: warning: unused variable 'skip' [-Wunused-variable]
int skip[UCHAR_MAX + 1]; /* スキップテーブル */
^
Main.c:11:6: warning: unused variable 'i' [-Wunused-variable]
int i;
^
Main.c:6:8: warning: variable 'pt' is used uninitialized whenever function 'bf_match' is called [-Wsometimes-uninitialized]
char *pt; /* txt をなぞるカーソル */
~~~~~~^~
Main.c:15:9: note: uninitialized use occurs here
while(*pt != '\0' && *pp != '\0'){
^~
Main.c:6:10: note: initialize the variable 'pt' to silence this warning
char *pt; /* txt をなぞるカーソル */
^
= NULL
Main.c:49:58: warning: format specifies type 'int' but the argument has type 'char *' [-Wformat]
printf("%d 文字目に見つかりました。\n", s+1);
~~ ^~~
%s
8 warnings generated.