Great. Post an outline. What you posted before had the wrong types
for that sort of design.
Here is the outline which gives run time error of the free() call in main():
/* A program that will ask the user for input and then will sort the input alphabetically
and will print it on stdout
* As of now it does not sort any words but it will very soon. I am writing it in parts
* when parts work fine,then we put them in one place
* Since I did not want to put any limitation on input size in this program, I have used
* dynamic memory allocation to solve the problem. My intent in creating and then solving
* this problem was purely of C learning (as defined by ANSI standard) and nothing else. I
* wanted to learn C and hence this problem and quite heavy discussion of it on comp.lang.c.
* I owe many thanks to the brilliant help I got in comp.lang.c
*
* VERSION 1.0
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
enum { WORD_SIZE = 3, WORD_ERR = -1 };
enum { GSW_OK, GSW_ENOMEM, GSW_ENORESIZE } ;
enum { GI_OK = 0 , GI_ERR = WORD_ERR };
int get_input( char**, size_t* );
int get_single_word( char** );
int main( void )
{
char* pword;
int input_err;
size_t words_count;
pword = NULL;
words_count = 0;
input_err = WORD_ERR; /* we will update the status in function call to get_input() */
input_err = get_input( &pword, &words_count );
if( WORD_ERR != input_err )
{
printf("words_count = %u\n", words_count);
}
/* sort_input(...)
printf_input(...) */
free( &pword );
return 0;
}
int get_input( char** pword, size_t* w_cnt )
{
int err_catcher;
*w_cnt = 0;
while( (err_catcher = get_single_word(pword)) && ( **pword != 0) )
{
if( GSW_OK == err_catcher )
{
printf("You entered: [%s]\n", *pword);
++*w_cnt;
}
else
{
return GI_ERR;
}
}
return GI_OK;
}
int get_single_word( char** ppc )
{
unsigned ele_num;
int ch;
size_t word_length, word_length_interval;
char* word_begin;
char* new_mem;
ele_num = 0;
word_length = WORD_SIZE;
word_length_interval = 2;
*ppc = malloc(word_length * sizeof(**ppc));
word_begin = *ppc;
if( NULL == word_begin )
{
return GSW_ENOMEM;
}
while( (EOF != (ch = getchar())) && isspace(ch) )
{
continue; /* Leading whitespace */
}
if( EOF != ch )
{
*word_begin++ = ch;
ele_num = 1;
}
while( (EOF != (ch = getchar())) && (! isspace(ch)) )
{
if( (word_length - 1) == ele_num++ )
{
new_mem = realloc( *ppc, (word_length_interval * word_length * sizeof *new_mem) );
if( new_mem )
{
word_begin = new_mem + (word_begin - *ppc);
word_length *= word_length_interval;
*ppc = new_mem;
}
else
{
*word_begin = '\0';
return GSW_ENORESIZE;
}
}
*word_begin++ = ch;
}
*word_begin = '\0';
return GSW_OK;
}