Hi All,
Can you spot an error in my code? It shows no warnings but causes a seg fault. I've isolated it down to the wrd[] lines in red. Rest of the code available if needed.
Thanks in advance.
Code:
char * fetchword( FILE* ifp, char* *wrd, int* wrd_count ) {
unsigned int c ; /*character to be added to end of current string*/
int wrd_len = 0 ; /*number of characters to be in new string*/
/*other stuff done here*/
*wrd = realloc( *wrd, wrd_len * sizeof(char) ) ; /*expand current array by one char*/
if( *wrd == NULL ) {
perror("realloc returned NULL. Unable to allocate memory.") ;
exit (-1) ;
}
*wrd[wrd_len-1] = c ; /*replace existing '\0' with new char*/
*wrd[wrd_len] = '\0' ; /*add the string terminator after new char*/
return *wrd ;