全局指针值丢失.2个c文件

Tom*_*Tom 3 c pointers global-variables

我使用3个文件制作trie:speller.c是主文件,dictionary.c包含函数和全局根trie指针,dictionary.h声明了字典的函数.在dictionary.c中完成函数加载后,全局根指针值丢失问题.

speller.c

int main(int argc, char* argv[])
{   
    // load dictionary
    char* dictionary = TMPDICT;

    bool loaded = load(dictionary);

    // abort if dictionary not loaded
    if (!loaded)
    {
        printf("Could not load %s.\n", dictionary);
        return 1;
    }

    //next I need to call check function, but root value is missing here

}
Run Code Online (Sandbox Code Playgroud)

dictionary.c

typedef struct TrieNode
    {
      struct TrieNode **children;
      bool is_word;
    } TrieNode;
struct TrieNode * root;


struct TrieNode *create_trienode(char c, struct TrieNode *parent);
bool load(const char* dictionary)
    {

        FILE * file = fopen(dictionary, "r"); 
        if (file == NULL) 
        {
            printf("trie: No such file(dictionary)\n"); 
            return 1;
        }

        struct TrieNode *root = create_trienode(' ', NULL);
        struct TrieNode *ptr = root; 
        int character;
        int converted;
        int buffer;

        //This handles if file does not end with a newline
        character = fgetc(file);
        buffer = fgetc(file);

        while(character != EOF)
        {
            character = tolower(character);
            if (character == 10) // if newline, start new word read
            {

            }
            else 
            if(isalpha(character))
            {
                converted = character - 'a'; 
                if(ptr->children[converted] == NULL)
                {
                    ptr->children[converted] = create_trienode(character, ptr); 
                }
            ptr = ptr->children[converted]; 
            }


        if (character == SLASHNUM) //if backslash
        {
            if(ptr->children[SLASH_TMP] == NULL)
            {
                ptr->children[SLASH_TMP] = create_trienode(character, ptr);
            }
            ptr = ptr->children[SLASH_TMP];
        }

        if(ptr != root && (!(character == SLASHNUM  || isalpha(character)) || buffer == EOF)) 
        {
            ptr->is_word = true;
            ptr = root;
            word_count++;
        }

          character = buffer;
          buffer = fgetc(file);
        }

       return true; 
    }


struct TrieNode *create_trienode(char c, struct TrieNode *parent)
{
    struct TrieNode *node = malloc(sizeof(struct TrieNode));
    node->children = malloc(ALPHABET_SIZE * sizeof(struct TrieNode*));
    node->is_word=false;

    for (int i = 0; i < ALPHABET_SIZE; i++)
    {
        node->children[i] = NULL;
    }

    return node; 
}
Run Code Online (Sandbox Code Playgroud)

GDB

Breakpoint 1, load (dictionary=0x80489c4 "tmpDict") at dictionary.c:100
100     character = fgetc(file);
(gdb) info locals
file = 0x804b008
root = 0x804b170
ptr = 0x804b170
character = 1267019363
converted = 262929407
buffer = 1266804984
(gdb) c
Continuing.

Breakpoint 2, main (argc=1, argv=0xbffff0d4) at speller.c:40
$1 = (struct TrieNode *) 0x0
(gdb)
Run Code Online (Sandbox Code Playgroud)

如何在speller.c中显示root?另外在任务中说我不能改变speller.c

Sou*_*osh 5

通常,您需要在dictionary.h文件中声明rootas 并在speller.c中包含该头文件.extern

这样一来,root将提供给翻译单元spleller.c.接下来,您可以使用rootinside speller.c,就像任何其他局部变量(在该文件本身中定义)一样.


[正如alk先生在下面的评论中正确指出]

这里的问题是局部 root变量的定义

 struct TrieNode *root = create_trienode(' ', NULL);
Run Code Online (Sandbox Code Playgroud)

load()函数内部实际上影响了全局root

struct TrieNode * root;`
Run Code Online (Sandbox Code Playgroud)

因此,全球root永远不会得到更新.

相关的,从C11标准,第6.2.1章(强调我的)

[...]如果标识符指定同一名称空间中的两个不同实体,则范围可能会重叠.如果是这样,一个实体(内部范围)的范围将严格地在另一个实体(外部范围)的范围之前结束.在内部范围内,标识符指定在内部范围内声明的实体; 在外部作用域中声明的实体在内部作用域内隐藏(并且不可见).