Tuesday, May 19, 2009

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

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

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( ) ;
}

No comments:

Post a Comment

Please Give Some Comment

Featured post

Sitecore 10 Installation on Docker