malloc/calloc/realloc

#include 
#include 
#include 
int main(void)
{
    int *p = NULL;
    p = (int *)malloc(5*sizeof(int));
    memset(p,0,5*sizeof(int));
    int i = 0;
    for(i = 0;i<5;++i)
    {
        printf("%d\n",*(p+i));
    }
    free(p);
    p=NULL;


    int *c = NULL;  
    c = (int *)calloc(5,sizeof(int));
    for(i = 0;i<5;++i)
    {
        printf("%d\n",*(c+i));
    }   
    free(c);
    c = NULL;

    int *r = NULL;
    for(i = 0;i<5;++i)
    {
        r = (int *)realloc(r,(i+1)*sizeof(int));
    }
    memset(r,0,5*sizeof(int));
    for(i = 0;i<5;++i)
    {
        printf("%d\n",*(r+i));
    }
    free(r);
    r = NULL;
    return 0;
}

你可能感兴趣的:(c)