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の危険性については,このサイトなど参考になる.

 
Link: Programming(4984d) Programming_C(5435d)
Last-modified: 2008-02-08 (金) 15:49:28 (5921d)