calloc、realloc 基本用法

作者在 2009-12-27 15:44:17 发布以下内容

#include "stdio.h"
#include "malloc.h"

int main(void)
{

 void i_o(int);
 int n,m;

 printf("input the number of datas:");
 scanf("%d",&n);

 printf("input the add number of datas:");
 scanf("%d",&m);

 i_o(n,m);
 putchar('\n');

 return 0;
}

void i_o(int n,int m)
{

 int i,*p;

 p=(int *)calloc(n,sizeof(int));//开辟内存空间

 printf("input the %d datas:\n",n);
 for(i=0;i<n;i++)
  scanf("%d",p+i);

 printf("output the %d datas:\n",n);
 for(i=0;i<n;i++)
  printf("%5d",*(p+i));

 p=(int *)realloc((int *)p,(n+m)*sizeof(int));//根据需要增加内存空间

 printf("\ninput again the add datas:\n");
 for(i=n;i<n+m;i++)
  scanf("%d",p+i);

 printf("reouput the %d datas:\n",n+m);
 for(i=0;i<n+m;i++)
  printf("%5d",*(p+i));

 free(p);
}

默认分类 | 阅读 794 次
文章评论,共0条
游客请输入验证码
文章分类