实现可变长数组的数组-征服C指针

来源:互联网 发布:肌肉发力原理知乎 编辑:程序博客网 时间:2024/05/20 00:36
/*使用指针数组实现可变长数组(参考征服C指针)p116*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void read_slogan(FILE *fp, char **slogan)
{
char buf[2014];
int i;
for ( i=0; i<7; i++)
{
fgets(buf,1024,fp);
/* 删除换行符*/
buf[strlen(buf)-1]='\0';

/*分配保存一个标语的内存空间*/
slogan[i]=malloc(sizeof(char)*(strlen(buf)+1));

/*复制标语的内容*/
strcpy(slogan[i],buf);
}
}


int main()
{
char *slogan[7];
int i;

read_slogan(stdin,slogan);

/*输出读取的标语*/
for(i=0; i<7; i++)
printf("%s \n",slogan[i]);

system("pause");
return 0;
}
0 0
原创粉丝点击