home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C by Discovery (4th Edition)
/
C_By_Discovery_4th_Edition.tar
/
C_By_Discovery_4th_Edition
/
_DISK_
/
ch11
/
malloc.c
< prev
next >
Wrap
C/C++ Source or Header
|
2005-06-16
|
2KB
|
44 lines
/* malloc.c
*
* Synopsis - Accepts input of a line of text, separates each
* of the blank-separated words in the line, and
* displays each word.
*
* Objective - To illustrate use of the malloc() function.
*/
/* Include Files */
#include <stdio.h>
#include <stdlib.h> /* Note 1 */
#include <string.h>
/* Constant Definitions */
#define BUF_SIZE 512
int main( void )
{
char instring[BUF_SIZE], *currentpl, *endword, *word;
printf( "Enter a line of text " );
printf( "with words separated with blanks:\n" );
fgets( instring, BUF_SIZE, stdin );
instring[ strlen( instring ) - 1 ] = '\0';
currentpl = instring;
/* Note 2 */
while (( endword = strchr( currentpl, ' ' )) != NULL ) {
*endword = '\0'; /* Note 3 */
/* Note 4 */
word = ( char * ) malloc( strlen( currentpl ) + 1 );
strcpy( word, currentpl );
printf( "I read that as \"%s\".\n", word );
currentpl = endword+1; /* Note 5 */
free (( void * ) word ); /* Note 6 */
}
/* Note 7 */
word = ( char * )malloc( strlen( currentpl ) + 1 );
strcpy( word, currentpl );
printf( "I read that as \"%s\".\n", word );
free(( void * ) word );
return 0;
}