/******************************************************/
/*
*/
/*
String storage allocation */
/*
*/
/******************************************************/
#include
<stdio.h>
/*
#include another file for malloc() and
*/
/*
strlen() ???. Check the compiler manual! */
#define
NOOFSTR 10
#define
BUFSIZE 255
#define
CODE 0
/******************************************************/
/*
Level 0 */
/******************************************************/
main
()
{
char *array[NOOFSTR], *malloc();
char buffer[BUFSIZE];
int i;
for
(i = 0; i < NOOFSTR; i++)
{
printf ("Enter string %d :",i);
scanf
("%255s",buffer);
array[i] = malloc(strlen(buffer)+1);
if (array[i] == NULL)
{
printf ("Can't allocate
memory\n");
QuitSafely (array);
}
strcpy (array[i],buffer);
}
for
(i = 0; i < NOOFSTR; i++)
{
printf ("%s\n",array[i]);
}
QuitSafely(array);
}
/******************************************************/
/*
Snakes & Ladders! */
/******************************************************/
QuitSafely
(array) /* Quit & de-alloc
memory */
char
*array[NOOFSTR];
{
int i, len;
for
(i = 0; i < NOOFSTR; i++)
{
len = strlen(array[i]) + 1;
if (free (array[i]) != 0)
{
printf ("Debug: free failed\n");
}
}
exit
(CODE);
}
0 comments:
Post a Comment