関数は strcpy として機能するはずですが、別の関数で使用できる文字列を使用することを理解しているため、独自の strdup 関数を作成しようとしましたが、プロトタイプでは、文字がコピーされない理由がわかりません。
このプロトタイプを作成しましたが、関数が返すポインターが文字列を表示しない理由がわかりません
char *my_strdup(char *str)
{
char *new_str;
char *to_copy;
int i;
to_copy = str;
i = strlen(str + 1);
new_str = malloc(sizeof(*new_str) * i + 1);
while(i - 1 > 0)
{
*new_str = *to_copy;
new_str++;
to_copy++;
i--;
}
return(new_str);
}
ここに私のテスト機能があります:
int main()
{
char *str;
str = my_strdup("helloo");
printf("%s\n", str);
}