Posts

Showing posts with the label Dynamic Memory Allocation

My Subject Blog

C Program for Memory Allocation and Reallocation

/* Write a program to store a character string in a block of memory space created by malloc and then modify the same to store a large string. */ #include <stdio.h> #include <stdlib.h> int main(void)  { char *ch; int n,n1; printf("\n Enter Size of String you Want : "); scanf("%d",&n); if((ch = (char *)malloc(n *sizeof(char))) == 0)         {             printf("\n No space available");             exit(1);         }     else       {           printf("\n %ld Bytes Space are allocated Successfully", n * sizeof(char));       }     printf("\nEnter String :");     scanf("%s",ch);     printf("\n String is : %s",ch);     printf("\n Enter Size You Want Increase: ");     scanf("%d",&n1);     if((c...

C Program for Runtime Memory Allocation for Table of Integers

/* Write a program that uses a table of integers whose size will be specified interactively at runtime.  */ #include <stdio.h> #include <stdlib.h> int main() {     int *p;     int n,i;     printf("\nEnter the size of table : ");     scanf("%d",&n);     if((p = (int*)malloc(n *sizeof(int))) == 0)        {            printf("\n No space available");            exit(1);        }    else       {          printf("\n %ld Bytes Space are allocated Successfully", n * sizeof(int));       }    for(i=0;i<n;i++)    {     printf("Enter Value for Position : [%d]",i+1);     scanf("%d",(p+i));    }    printf("\n Values in Reverse \n ");     for(i=n-1;i>=0;i--) ...

Followers