0

嗨,我得到一个符号 NULL 未解决的错误我已经尝试再次保存、清理和构建项目。我也试过关闭并重新启动 Eclipse。我已经包含了我认为我需要的库以及更多,但是我有 13 个这些错误加上一个 stderr 未解决的错误,任何人都可以在这里指出我的问题。本来想发图的,但是我的rep不够高。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stddef.h>

#define PUNC    " \t\n\r,;.:!()[]{}?'\""
typedef struct node node;

typedef struct node {
    char *word;
    int count;
    node *left;
    node *right;
} node;
void insert( node ** dictionary, char * word ) {
    int result;
    node * entry;
        if ( word == NULL || dictionary == NULL ) return;
        if ( *dictionary == NULL ) {
            entry= (node *) malloc( sizeof( node ) );
            strcpy( entry->word= (char *) malloc( strlen( word ) + 1 ), word );
            entry->left= entry->right= NULL;
            entry->count= 1;
            *dictionary= entry;
            return;
        }
        result= strcmp( word, (*dictionary)->word );
        if ( result < 0 )
            insert( &(*dictionary)->left, word );
        else if ( result > 0 )
            insert( &(*dictionary)->right, word );
        else
            ++(*dictionary)->count;
    return;
}
void printDictionary( node * dictionary ) {
        if ( dictionary == NULL ) return;
        printDictionary( dictionary->left );
        printf( "%s = %d\n", dictionary->word, dictionary->count );
        printDictionary( dictionary->right );
    return;
}
void freeDictionary( node ** dictionary ) {
        if ( dictionary == NULL || *dictionary == NULL ) return;
        freeDictionary( &(*dictionary)->left );
        freeDictionary( &(*dictionary)->right );
        free( (*dictionary)->word );
        free( *dictionary );
        *dictionary= NULL;
    return;
}
int main( int argc, char *argv[] ) {
    FILE *fp;
    char b[1000], *s;
    node *dictionary= NULL;
    int i;
        for ( i= 1; i < argc; ++i ) {
            if ( (fp= fopen( argv[i], "r" )) == NULL ) {
                fprintf( stderr, "File %s can not be opened.\n", argv[i] );
                continue;
            }
            for( s= fgets( b, sizeof(b), fp ); s != NULL; s= fgets( b, sizeof(b), fp ) ) {
                char *word;
                for ( word= strtok( b, PUNC ); word != NULL; word= strtok( NULL, PUNC ) )
                    insert( &dictionary, strlwr( word ) );
            }
            fclose( fp );
        }
        printDictionary( dictionary );
        freeDictionary( &dictionary );
    return 0;
}
4

0 回答 0