write a c program to allocate dynamic memory for three string scan it and print it

16 Dec 2022 Balmiki Mandal 0 C Programming

"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:

  1. 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.

  2. 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.

  3. 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

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.