write a c program to allocate dynamic memory for three string scan it and print it
"Write a C program to allocate dynamic memory for three strings, scan them, and print them" means to write a program in the C programming language that performs the following tasks:
-
Dynamically allocate memory for three strings: This means that the program should request memory space from the operating system at runtime to store three strings.
-
Scan the three strings: This means that the program should prompt the user to input three strings and store them in the memory space allocated in step 1.
-
Print the three strings: This means that the program should retrieve the three strings stored in the memory space allocated in step 1 and print them to the console.
Overall, this program should demonstrate how to allocate dynamic memory in C, how to scan user input for three strings, and how to print the three strings to the console.
c program to allocate dynamic memory for three strings scan it and print it
#include<stdio.h>
#include<stdlib.h>
void main()
{
char *p[3];
int i;
for (i=0;i<3;i++)
p[i]=malloc(sizeof(char)*10);
printf("Enter the String:");
for(i=0;i<3;i++)
scanf("%s",p[i]);
for(i=0;i<3;i++)
printf("%s\n",p[i]);
}
Output:
Enter the String: Electro4u is Best
Electro4u is Best