20140101【C言語】mallocを使う
お題
mallocを使う
プログラム概要
80バイトをmallocで確保し、その領域に文字列を入力させ
入力された文字列を表示する
ソース
#include
#include
int main(void)
{
char *p;
p = (char *)malloc(80);
if (!p) {
printf("Memory alloc failure.\n");
exit(1);
}
printf("Please input string: ");
gets(p);
printf(p);
free(p);
return 0;
}
実行結果
# ./a.out Please input string: hoge hoge