Surely you mean *pw = calloc( arr_size, sizeof **pw );
Oh, by the way - I don't see the bit where you point the array's elements
to words. Why isn't get_words calling get_single_word?
Okay here is my new code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
enum { WORD_SIZE = 28, ARRSIZE = 10 };
enum { GSW_OK, GSW_ENOMEM, GSW_ENORESIZE } ;
enum { GW_OK, GW_ENOMEM, GW_ENORESIZE };
int get_words( char***, size_t* );
int get_single_word( char** );
int main( void )
{
char** pword; /* pw means pointer to word */
size_t word_count;
int num;
pword = NULL;
word_count = 0;
num = get_words( &pword, &word_count );
printf("words = %d, num = %d\n", (int) word_count, num);
free(pword);
return 0;
}
int get_words( char*** pw, size_t* cnt )
{
size_t idx;
int mem_check;
char **pw_begin, **new_mem;
char* pword;
size_t arr_size;
arr_size = ARRSIZE;
mem_check = GW_ENOMEM;
*pw = calloc( arr_size, sizeof **pw );
pw_begin = *pw;
if( *pw )
{
mem_check = GW_OK;
pw_begin = *pw;
for( idx = 0; (! get_single_word( &pword )) && *pword; ++idx )
{
if( idx == arr_size )
{
new_mem = realloc( **pw, (2 * arr_size * sizeof new_mem));
if( new_mem )
{
pw_begin = new_mem;
}
else
{
mem_check = GW_ENORESIZE;
}
}
if( GSW_OK == mem_check )
{
pw_begin++ = &pword;
}
}
pw_begin = NULL;
}
*cnt = idx;
return mem_check;
}
int get_single_word( char** pc )
{
int mem_check;
unsigned idx;
int ch;
size_t curr_size;
char *new_mem;
char *pc_begin;
mem_check = GSW_ENOMEM; /* will updat eit on first use */
curr_size = WORD_SIZE;
*pc = calloc(curr_size, sizeof(**pc));
if( (! *pc) )
{
mem_check = GSW_ENOMEM;
return mem_check;
}
else
{
mem_check = GSW_OK;
pc_begin = *pc;
}
while( (ch = getchar()) && (isspace( (unsigned char) ch)) )
{
continue; /* skip leading white space */
}
if( EOF != ch )
{
*pc_begin++ = ch;
}
while( (GSW_OK == mem_check) &&
((ch = getchar()) != EOF) &&
(! isspace( (unsigned char) ch)) )
{
if( idx == curr_size )
{
new_mem = realloc( pc_begin, (2 * curr_size * sizeof(*new_mem)));
if( new_mem )
{
pc_begin = new_mem;
curr_size *= 2;
}
else
{
mem_check = GSW_ENORESIZE; /* error, couldn't not enlarge */
*pc_begin = '\0';
return mem_check;
}
}
if( GSW_OK == mem_check )
{
*pc_begin++ = ch;
}
}
*pc_begin = '\0';
return mem_check;
}
======================= OUTPUT ========================
[arnuld@dune ztest]$ gcc -ansi -pedantic -Wall -Wextra sort-input.c
sort-input.c: In function `get_words':
sort-input.c:79: error: invalid lvalue in assignment
[arnuld@dune ztest]$
Line 79: pw_begin++ = &pword;
I don't understand why I am getting this error.