特急 100分 关于一个小程序

benjimmyvip 2003-01-22 01:12:07
采用C编写程序:
动态分配一段缓冲区,保存26个英文字母,并从缓冲区中读出26个字母,
循环在屏幕上输出20遍,每行输出20各字母
...全文
25 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yellowwolf 2003-01-26
  • 打赏
  • 举报
回复
heyesuperman(荷花)这样的代码看的好累。感觉不舒服。
heyesuperman 2003-01-26
  • 打赏
  • 举报
回复
我得意见是:#include <iostream.h>
#include <stdio.h>
void main()
{
int *p;
p=new int('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
for(int i=0;i<20;i++)
{for(int j=0;j<26;j++)
{printf("%s ",p[j]);
while(j=20) cout<<"\n";
}
}

}
不知可否。
亮剑_ 2003-01-26
  • 打赏
  • 举报
回复
没有 free 就是内存泄漏了。不良代码,会依赖编译器的
xdspower 2003-01-26
  • 打赏
  • 举报
回复
如果不加,有的编译器会优化为程序退出时回收。
Cybergate 2003-01-26
  • 打赏
  • 举报
回复
不加free(buff),不知道操作系统能不能自动回收......
Cybergate 2003-01-26
  • 打赏
  • 举报
回复
也许这只是个练习题,动态分配知识考考你会不会用.

#include <stdlib.h>
#include <stdio.h>

main()
{
char *buff;
int i;
buff=(char*)malloc(26*sizeof(char));
for (i=0; i<26; i++) buff[i]=(char)(i+'A');
for (i=0; i<26*20; i++) {
putchar(buff[i%26]);
if (i%20==19) putchar('\n');
}
}
shangpin 2003-01-26
  • 打赏
  • 举报
回复
bugfree(八个飞飞)

buf[i]=rand()%26 +97;

这句话是什么意思?
lw549 2003-01-26
  • 打赏
  • 举报
回复
这需要动态分配吗?
我觉得只分配一次就可以了.
River_H 2003-01-22
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <conio.h>

void main()
{
int nCount, nCount1;
char *pBuffer = (char*)malloc(26);

for (nCount = 0; nCount < 26; nCount++)
{
*(pBuffer + nCount) = 'a' + nCount;
}

for (nCount = 0; nCount < 20; nCount++)
{
for (nCount1 = 0; nCount1 < 26; nCount1++)
{
printf("%c ", *(pBuffer + nCount1));
if (nCount1 == 20)
{
printf("\n");
}
}
printf("\n");
getch();
}
free(pBuffer);
}
输出一遍后,按一个键再输出另一遍
bugfree 2003-01-22
  • 打赏
  • 举报
回复
See the following function: f()
======

#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <stdlib.h>

int f()
{
int i,j;
char *buf = malloc(27);

srand(time(0));
for(j=1; j<20; j++)
{
for(i=0; i<26; i++) buf[i]=rand()%26 +97;
buf[26]='\0';
printf("%s\n", buf);
}
free(buf);
return 0;
};
sunwhite 2003-01-22
  • 打赏
  • 举报
回复
用C是可以的,malloc分配缓冲区,用完后用free释放掉。
beyondsky00 2003-01-22
  • 打赏
  • 举报
回复
用c++可以吗?
我觉得用new方便一点!

70,032

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧