セグメンテーション違反が発生します
そのため、上部にtypedef char*stringがあります。
それから私はちょうど約5に設定されているスペースと呼ばれる変数を持っています
for(i=0; i<=spaces; i++) {
sepwords[i] = malloc(3000);
}
strはchar配列です。スペースを探して、それまでコピーしています。
while(str[i]!=' ') {
printf("%d\n", i);
strcpy(sepwords[i], "hello");
i++;
}
実際に機能するように
しかし、私がそうするなら
while(str[i]!=' ') {
char *temp[100];
*temp=str[i];
printf("%d\n", i);
strcpy(sepwords[i], *temp);
i++;
}
これで障害が発生します
事前にメモリを割り当てていたので、typedef文字列を使用しているためではないと思います。
何か案は?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 256
char *progn;
void usage(void) {
fprintf(stderr, "Usage: %s pattern\n", progn);
}
typedef char * string;
int pattern_match(char *pattern, char *str) {
int i=0;
int spaces=0;
while(str[i]!=0) {
if(str[i]==' ') {
spaces++;
}
i++;
}
string sepwords[spaces];
for(i=0; i<=spaces; i++) {
sepwords[i] = malloc(3000);
}
i=0;
while(str[i]!=' ') {
char *temp[100];
*temp=str[i];
printf("%d\n", i);
strcpy(sepwords[i], temp);
i++;
}
//printf("%d\n", spaces);
//strs[0]="hiya boy";
//printf(strs[1]);
}
int main(int argc, char **argv) {
char line[MAXLINE];
char *pattern;
progn = argv[0];
if (argc != 2) {
usage();
return EXIT_FAILURE;
}
pattern = argv[1];
while (!feof(stdin) && !ferror(stdin)) {
if (!fgets(line, sizeof (line), stdin)) {
break;
}
if (pattern_match(pattern, line)) {
printf("%s", line);
}
}
if (ferror(stdin)) {
perror(progn);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}