Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Program to allocate memory dynamically for strings, and store their addresses in array of pointers to strings

Posted By: Nixie Schmidt     Category: C Programming     Views: 7510

Program to allocate memory dynamically for strings, and store their addresses in array of pointers to strings.

Code for Program to allocate memory dynamically for strings, and store their addresses in array of pointers to strings in C Programming

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#include <string.h>

void main( )
{
    char *name[5] ;
    char str[20] ;
    int i ;

    clrscr( ) ;

    for ( i = 0 ; i < 5 ; i++ )
    {
        printf ( "Enter a String: " ) ;
        gets ( str ) ;
        name[i] = ( char * ) malloc ( strlen ( str ) + 1 ) ;
        strcpy ( name[i], str ) ;
    }

    printf ( "\nThe strings are:" ) ;

    for ( i = 0 ; i < 5 ; i++ )
        printf ( "\n%s", name[i] ) ;

    for ( i = 0 ; i < 5 ; i++ )
        free ( name[i] ) ;

    getch( ) ;
}
  
Share: 



Nixie Schmidt
Nixie Schmidt author of Program to allocate memory dynamically for strings, and store their addresses in array of pointers to strings is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!