gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:06:21 27 4
gpt4 key购买 nike

我正在使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com