Experiment Number 3.3: Aim of The Experiment
Experiment Number 3.3: Aim of The Experiment
PROGRAM TO BE DONE: -
WAP to store a character string in block
of memory space created by malloc and then modify the same to
store a large string.
FLOWCHART/ ALGORITHM: -
● firstly, we will define header file
● then we include main body.
● Then we define the data types.
● Then we use scanf and printf function to take input and
statement output respectively.
● Then we use malloc function and puts function to allocate
the number of bytes in memory and to show the output
respectively.
● Then we use reallocate function and for loop .
● Atlast, we print the output using printf function.
PROGRAM CODE: -
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i;
printf("\tRUNTIME MEMORY ALLOCATION\n");
printf("Enter max number of characters you want to input\n");
scanf("%d",&n);
char *p = (char*)malloc(n*sizeof(char));
if(p==NULL)
{
printf("Memory allocation fails..");
exit(0);
}
puts("Enter string please");
for(i=0;i<n;i++)
{
scanf("%c",p+i);
}
//Now terminate string with NULL character
*(p+i)= '\0';
printf("You wrote %s",p);
fflush(stdin);
printf("\nEnter new size\n");
scanf("%d",&n);
p= realloc(p,n*sizeof(char));
puts("\nEnter new string please");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%c",p+i);
}
//Now terminate string with NULL character
*(p+i)= '\0';
printf("You wrote %s\n",p);
free(p);
return 0;
}
OUTPUT
LEARNING OUTCOMES
● Apply various constructs, loops, functions to solve mathematical and scientific problem.
● Design and develop modular programs for real world problems using control structure
and selection structure.