Cでメモリを節約して文字列を読む の変更点 - アールメカブ

アールメカブ


Cでメモリを節約して文字列を読む の変更点


[[Programming]]


 #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の危険性については,[[このサイト:http://kitaj.at.infoseek.co.jp/fgets.html]]など参考になる.