C言語なら、俺に聞け! <14>
■ このスレッドは過去ログ倉庫に格納されています
0846840
NGNGkey_stringに含まれるlookforをreplaceに置換するというプログラム
です。実はまだよくポインタとかわかってない厨房なんですが、参考書や
WEBにあるサンプルを参考にしながら作ってみました。
void ReplaceTag(char *key_string, const char *lookfor, const char *replace)
{
char *reference_string, *sub_string;
int key_len, replace_len, hit_to_top, temp_len = 0;
sub_string[0] = '\0';
key_len = strlen(key_string);
replace_len = strlen(replace);
while(1) {
//lookforがある位置を検索
reference_string = strstr(key_string, lookfor);
if (reference_string == NULL ) {
realloc(sub_string, strlen(sub_string) + strlen(key_string) + 1);
strcat(sub_string, key_string);
break;
}
//lookforの1つ前までの長さを取得
hit_to_top = key_len - strlen(reference_string) - temp_len;
//メモリの再割り当て
realloc(sub_string, hit_to_top + strlen(sub_string) + 1);
//lookforが見つかった場所の1つ前までをコピー
strncat(sub_string, key_string, hit_to_top);
strcat(sub_string, replace);
reference_string ++;
key_string += hit_to_top;
temp_len = (hit_to_top + 1);
}
key_string = malloc(strlen(sub_string) + 1);
strcpy(key_string, sub_string);
free(sub_string);
}
コンパイルは通るのですが、実行時にエラーになります。ドコで
コケてるのかすらわかりません。もう3日ほど費やしたのですが。
どうかご教授ください。
■ このスレッドは過去ログ倉庫に格納されています