#include <stdio.h>
#include <stdlib.h>

/*The realloc function in C is used to dynamically change the size of a
previously allocated block of memory. It allows you to resize the memory
block to either expand or shrink it.*/

int main(){
    int *ptr;
    int n=1,n2=2;
    
    ptr=(int*)calloc(n,sizeof(int));
    
    for(int i=0;i<n;i++){
        scanf("%d",ptr+i);
    }
    
    ptr=realloc(ptr,n2*sizeof(int));
    
    for(int i=0;i<n2;i++){
        printf("%d ",*ptr+i);
    }
}

#if 0
int main(){
    int *ptr,i,n1,n2;
    printf("Enter the size of n1 : ");    
    scanf("%d",&n1);
    
    ptr=(int*)malloc(n1*sizeof(int));
    
    for(i=0;i<n1;i++){
        printf("%pc\n",ptr+1);
    }
    
    printf("Enter the size of new n2 : ");
    scanf("%d",&n2);
    
    ptr=realloc(ptr,n2*sizeof(int));
    
    for(i=0;i<n2;i++){
        printf("%pc\n",ptr+1);
    }
    
    free(ptr);
}
#endif

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: