realloc() Function in C

Get Certified in C Programming and Take Your Skills to the Next Level

Program 1

//Program for realloc function
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#define clrscr() system("cls")
int main()
{
        int *ptr=NULL;
        ptr=(int *)malloc(2*sizeof(int));
       // clrscr();
        if(ptr==NULL)
        {
            printf("Memory not allocated");
            //exit(0);
        }
        else
        {
            printf("Enter two Number");
            for(int i=0;i<2;i++)
            {
                scanf("%d",ptr+i);
            }
            ptr=(int*)realloc(ptr,4*sizeof(int));
            if(ptr==NULL)
           {
            printf("Memory not allocated");
            //exit(0);
           }
          else
          {
            printf("Enter next two Number");
            for(int i=2;i<4;i++)
            {
                scanf("%d",ptr+i);
            }
            int sum=0;
            printf("Elements are: ");
            for(int i=0;i<4;i++)
            {
              printf("%d\n",*(ptr+i));
               sum=sum+*(ptr+i);
            } 
            printf("Sum is %d",sum);
            free(ptr);
         }   
        }
}

 

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *