Hi all,
This is a university project that compares linked list and hash table efficiencies (eventually). The problem here I believe is my use of the pointers. Can anyone give me some advice on why I'm going wrong? It seems to have problems when it encounters 'strncat' in the apendchar function, but I can't see why. I'm supposed to be writing to C90 standard. I've included the compiler warnings at the bottom.
Thanks in advance!
Code:
typedef enum { FALSE, TRUE } boolean ;
#include "stdio.h"
#include "stdlib.h" /*calloc*/
#include "ctype.h" /*isalnum*/
#include "string.h" /*string functions*/
/*#include "time.h" /*Execution time*/ */
#include "grammar.c" /*additional grammar checks for - and ' */
/*Places addtional letters at the end of the current word*/
char * apendchar( unsigned int *ch, char *wrd, int *wrd_len ) {
wrd = (char*)realloc(*wrd, (*wrd_len+1)*sizeof(char));
if( wrd == NULL ) {
perror("realloc returned NULL. Unable to allocate memory.") ;
exit (-1) ;
}
strncat( wrd, *ch, 1 ) ;
return wrd ;
}
/*Grabs a single word at a time from the input file*/
char * fetchword( FILE * ifp, char *wrd, int *wrd_count ){
unsigned int c ;
int wrd_len = 0 ;
boolean prev_char = FALSE ; /*remember validity of previous letter read for grammar checking*/
do{ /*loop until EOF*/
c = getc( ifp ) ;
if( isvalid( &c, ifp, &prev_char ) ) {
wrd_len++ ;
c = tolower( c ) ;
*wrd = apendchar ( &c, wrd, &wrd_len ) ;
prev_char = TRUE ;
}
/*increment wrd_count at the end of each word*/
if( !isvalid( &c, ifp, &prev_char ) && c != EOF ) {
*wrd_count++ ;
return wrd ;
}
}while( c != EOF ) ;
/*Mustn't forget the EOF case*/
return wrd ;
}
/*takes file from command prompt*/
int main( int argc, char *argv[] ) {
FILE *ifp = NULL ; /*in-file pointer*/
char *wrd ;
int wrd_count = 0 ;
/*read file from command prompt. fopen returns NULL if error. */
ifp = fopen(argv[1], "r");
if( ifp == NULL ) {
perror("fopen returned NULL. Check spelling or file location.") ;
exit (-1) ;
}
do{ /*loop until EOF*/
wrd = NULL ;
wrd = fetchword( ifp, &wrd, &wrd_count ) ;
/*do other stuff*/
} while ( wrd != NULL ) ;
printf( "\n%d word(s) read.\n", wrd_count ) ;
fclose( ifp ) ;
return 0 ;
}
Code:
boolean isvalid ( unsigned int *test_char, FILE * ifp, boolean *prev_char ) {
unsigned int test;
/*All alpha-numeric characters are valid input*/
if ( isalnum( *test_char ) && *test_char != EOF )
return TRUE ;
/*return true only if - and ' are part of an abbreviation such as: test-case or they'll*/
if ( *prev_char && ( *test_char == '-' || *test_char == '\'') && *test_char != EOF ) {
test = getc( ifp ) ;
if ( isalnum( test ) ) {
ungetc( test, ifp ) ;
return TRUE ;
} else {
return FALSE ;
}
}
return FALSE ;
}
GCC Warnings:
Code:
In function ‘apendchar’:
40: warning: passing argument 1 of ‘realloc’ makes pointer from integer without a cast
45: warning: passing argument 2 of ‘strncat’ makes pointer from integer without a cast
In function ‘fetchword’:
59: warning: assignment makes integer from pointer without a cast
65: warning: value computed is not used (wrd_count)
In function ‘main’:
89: warning: passing argument 2 of ‘fetchword’ from incompatible pointer type