C程序设计(第四版) 谭浩强 习题8.19(2) 个人设计
习题 8.19(2)写一函数free,将前面用new函数占用的空间释放。free(p)表示将p(地址)指向的单元以后的内存段释放。
代码块:
#include <stdio.h>
#include <stdlib.h>
char *neww(int n);
void freee(char *p);
int main()
{
int num;
char *n;
printf("Please enter number: ");
scanf("%d", &num);
n=neww(num);
scanf("%s", n);
puts(n);
freee(n);
system("pause");
return 0;
}
char *neww(int n)
{
char *p;
p=(char *)malloc(n*sizeof(char));
return p;
}
void freee(char *p)
{
free(p);
}