Dynamic Memory Allocation in C

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

Program 1

// Program for  Dynamic memory
#include<stdio.h>
#include<conio.h>
int main()
{
       
          int *ar,n,i,s,f=0;
          system("cls");
          printf("Enter size of an array");
          scanf("%d",&n);  
          ar=(int*)calloc(sizeof(int),n);   // allocated dynamic memory
          printf("\nEnter elements in array");
          for(i=0;i<n;i++)
          scanf("%d",&ar[i]);
          printf("\n Elements are: ");
          for(i=0;i<n;i++)

          printf("%d  ",ar[i]);
          printf("\nEnter an element for search");
          scanf("%d",&s);
          for(i=0;i<n;i++)
          {
              if(ar[i]==s)
              {
                  f=1;
                 break;
              }  
          }
         if(f==1) 
           printf("\n searching success");
         else
         printf("\n searching not success");  

        free(ar);    // deallocated dynamic memory
return 0;
}

 

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience 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 *