/*输入:2
12345
abcde
希望输出:
0:12345
1:abcde
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
char **instr, **p;
int i, j;
printf( "num: " );
scanf( "%d", &j );//不能有"\n"
if( !j )
return 0;
//fflush( stdin ); // 论坛上讲用fflush() 或 setbuf()都能达到同样的结果,但我用fflush()没有实现.
setbuf(stdin,NULL);
instr = (char **)malloc( sizeof(char *) * j );
if( !instr )
return 1;
memset( instr, 0, sizeof(char *) * j );
for( i = 0, p = instr; i < j; i++ )
{
printf( "i=%d:", i );
*p = (char *)malloc( sizeof(char) * 80 );
if( !*p )
return 1;
fgets( *p,80,stdin );//从标准输入读入一行字符串,包括回车换行符"\n"
(*p)[strlen(*p)-1] = '\0';
*p++;
}
for( i = 0, p = instr; i < j; i++ )
{
printf( "%d: %s\n", i, *p );
free( *p++ );
}
free( instr );
return 0;
}