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

标签 c pointers global-variables

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

拼写器.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

}

字典.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; 
}

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)

如何使 root 在 speller.c 中可见?同样在任务中说我不能改变speller.c

最佳答案

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

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


[正如 Mr. alk 正确指出的那样在下面的评论中]

这里的问题是本地 root 变量的定义

 struct TrieNode *root = create_trienode(' ', NULL);

load() 函数内部 shadows 全局 root

struct TrieNode * root;`

因此,全局 root 永远不会更新。

相关,来自 C11 标准,章节 §6.2.1(强调我的)

[...] If an identifier designates two different entities in the same name space, the scopes might overlap. If so, the scope of one entity (the inner scope) will end strictly before the scope of the other entity (the outer scope). Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.

关于c - 全局指针值丢失。 2个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32308452/

相关文章:

c - 如何缓冲和延迟 printf() 输出?

c - 在c函数中传递指针值的问题

objective-c - 将枚举与 NSNumber 进行比较

Javascript 全局变量未初始化

c - R:使用带有 .Call 和 C/C++ 包装器而不是 .Fortran 的 Fortran 子例程的优点?

c - 为什么这个字符串反转 C 代码会导致段错误?

c - 如何在循环外部结构的同时循环结构内部的结构 C 程序

c++ - 指针++,未定义的行为

javascript - 从不同的函数访问变量而不创建全局变量

c++ - 跨多个对象使用 map