C PROGRAMS : DYNAMIC MEMORY ALLOCATION

C PROGRAMS : DYNAMIC MEMORY ALLOCATION
1. Program to dynamically allocate memory using calloc for "n" integer elements, input elements, display it and then free the memory
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)calloc(n,sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

2. Program to dynamically allocate memory using malloc for "n" integer elements, input elements, display it and then free the memory
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

3. Program to dynamically allocate memory using malloc for "n" integer elements, input elements and then display it. Then reallocate memory for the same pointer variable, input elements, display it and then free the memory
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i;
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
 printf("\nEnter number of elements : ");
    scanf("%d",&n);
    
    // Reallocating memory 
 ptr=realloc(ptr,n);
 
 // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
 // Deallocating memory
 free(ptr);
    
    getch(); // Linux user - Remove this

    return 0;
}

4. Program to find the length of the string
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>
void main()
{
 char *ptr;
 
 // Declaring variable i=to iterate loop, l=lenght of string
 int i, l=0;
 
    // Allocating memory
 ptr=(char *)malloc(12*sizeof(char));
 
 // Inputing string
 printf("Enter string : ");
 gets(ptr);
 
 // Displaying string
 printf("Entered string : ");
 puts(ptr);
 
 // Calculating length
 while(ptr[l]!='\0')
 {
 l++;
 }
 
 // Printing length of string
 printf("Lenght of string : %d",l);
 
 // Deallocating memory
 free(ptr);
 
 getch(); // Linux user - Remove this

 return 0;
}

Share this

Related Posts

Previous
Next Post »