作业帮 > 综合 > 作业

英语翻译Write a function that dynamically allocates an array of

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/14 17:46:16
英语翻译
Write a function that dynamically allocates an array of integers.The function should accept an integer argument indicating the number of elements to allocate.The function should return a pointer to the array.
刚写的一个程序,连调用示例都帮你写好了.呵呵.其中AllocArray()这个函数就是你想要的.vc下编译通过.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int * AllocArray(int size)
{
    int *p = (int*)malloc(size*sizeof(int));
    if(!p) {        /*错误处?/
        printf("AllocArray:malloc memory err!\n");
        return NULL;
    }
    return p;
}

main()    /*测试AllocArray*/
{
    int *m;
    m=AllocArray(10);    /*这里我申请了10个元素的数组*/
    memset(m,0x00,sizeof(int)*10);   /*将它们初始化为0,注意长度是sizeof(int)*10*/
    free(m);    /*我又将它释放掉了.呵呵*/
}