#include <stdio.h>
#include <malloc.h>
#include <process.h>
void main( )
{
int count;
int *array;
if(( array = (int *) malloc (10 * sizeof(int))) == NULL)
{
printf("不能成功分配存储空间");
exit(1);
}
for (count=0;count<10;count++)
array[count]=count;
for(count=0;count<10;count++)
printf("%2d",array[count]);
free(array);
}
在网上看到的一段错误代码,在这里改过贴上去
本文通过一个C语言程序示例介绍了如何使用malloc函数进行动态内存分配,并展示了如何初始化和释放分配的内存。该程序创建了一个整数数组并依次赋值,最后输出数组内容并释放内存。
2725

被折叠的 条评论
为什么被折叠?



