Cでメモリを節約して文字列を読む
#include<stdio.h> #include<stdlib.h> #include<string.h> int main(){ char *buff; int *a; buff = (char*) malloc(100); fgets(buff, 100, stdin); a=(int*)malloc(strlen(buff)+1); strcpy(a,buff); printf("入力文字: %s\n",a); free(buff); free(a); return 0; }
gets()を使うと警告がうるさいので,fgets()を使っている.getsの危険性については,このサイトなど参考になる.
Link: Programming(5321d)
Programming_C(5772d)
Last-modified: 2008-02-08 (金) 15:49:28 (6258d)