Perform a task with realloc statement | C - Program

realloc( ) statement:

 

Code:

#include<stdio.h>

#include<stdlib.h>

void main()

{

    int n,*p,i,*q;

    printf("Enter the size of elements:\n");

    scanf("%d",&n);

    p=(int*)malloc(n);

    printf("Enter the elements:\n");

    for(i=0;i<n;i++)

        scanf("%d",p+i);

    printf("The elements are:\n");

    for(i=0;i<n;i++)

        printf("%d\t",*(p+i));

    printf("\nEnter the new size:\n");

    scanf("%d",&n);

    q=realloc(p,n);

    printf("Enter the new elements:\n");

    for(i=0;i<n;i++)

        scanf("%d",q+i);

    for(i=0;i<n;i++)

        printf("%d\t",*(q+i));



}

Output:

 Comment for your feedback!

Comments

Popular posts from this blog

TOP 10 PROGRAMMING LANGAUAGES COMMING 2021 | ARTICLE

Write a C-program to swap two number:

Print Fibonacci Series using Recursion | C program :