gpt4 book ai didi

c - 链表 - C - 从节点指针数组正确访问结构节点的值

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:49 24 4
gpt4 key购买 nike

第一篇文章,编码知识极其有限,而且是 C 语言的新手。温柔点!我正处于“尝试”不同的事情让我越来越困惑的地步。我需要有人的正确指导!

这个特殊问题来 self 正在尝试的在线 edX 类(class),该类(class)最终在正确实现后会检查从文本文件中读入的给定单词(“检查”功能)并将其与读入的每个单词(从 ' load' 函数)结构的链表。

我相信我在使用 gdb 时正确地实现了加载函数,正如我在逐步执行它时所看到的那样,但我的问题和我的问题特别与检查函数有关。我还有很多东西要实现才能完成我的代码,但是在使用 gdb 进行测试时,我没有看到结构的 char* 成员的值与我预期应该看到的值一致。

  1. 当使用 gdb 并单步执行“检查”函数并尝试访问我在加载函数中创建的链表中结构节点的 dword 成员时,我预计我应该查看 char* 成员的字符串。例如,我预计将单词“cat”分配给 current->dword ,但我在测试时却在 gdb 中看到:

    ~(gdb) 打印当前->双字

    $13 = 0xbfffede2 "\004\b\214\365\372D\300\355\377\277"

我的想法是,我仍然只是以某种方式访问​​一个地址,而不是实际值,但我不知道为什么会这样。当在加载函数中创建节点时,一个值被正确地分配给 dword 成员(至少据我所知,在逐步执行 gdb 中的代码时)但似乎不是在检查功能中正确访问。对新手的任何帮助将不胜感激!

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

#include "dictionary.h"

typedef struct node
{
char* dword;
struct node* next;
}
node;

// keep track of #of words in dictionary loaded
int wordCounter = 0;

// create root for hash table
node* root[26];

// create cursor to keep place in creating, pointing, and traversing through nodes
node* current = NULL;

/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
// size of word read into buffer
int wordSize = sizeof(word);

// prepare to make a new lowercase only word for comparison to lowercase only dictionary
char bufWord[wordSize];

// make it
for(int i = 0; i < wordSize; i++)
{
if (i == wordSize - 1)
{
bufWord[i] = '\0';
}

else
{
bufWord[i] = tolower(word[i]);
}
}

// hash word to achieve proper root node location
int hash = bufWord[0] - 97;

// point to the correct root node to begin traversing
current = root[hash];

// make sure there is even a word in hash table location
if(root[hash] == NULL)
{
return false;
}

else if(root[hash] != NULL)
{
// progress through the nodes until the last node's next pointer member is NULL
while(current != NULL)
{
// compare 1st letter only of current->dword[i] to bufWord[i] to save time

// if they don't match, return false

// if they do match then continue
\
char dictWord[wordSize];

// hold copy of struct member value to compare to dictWord
char* wordTemp = current->dword;

//
for(int i = 0; i < wordSize; i++)
{
dictWord[i] = wordTemp[i];
}

// do a spell check
if(strcmp(bufWord, dictWord) == 0)
{
return true;
}

else
{
// set current to the next node if any or NULL if it's already the last node in the list
current = current->next;
}
}
}

return false;
}

/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
// buffer for reading in dictionary words
char wordIn[LENGTH + 1];

// open the dictionary file
FILE* newDict = fopen(dictionary, "r");

for (int i = 0; i < 27; i++)
{
root[i] = NULL;
}

// while there are words to read
while(fscanf(newDict, "%s ", wordIn) > 0)
{

// keep track of #of words for constant time read in size function
wordCounter++;

// hash the first letter for the location in root
int hash = wordIn[0] - 97;

// malloc space for a new node
node* newNode = malloc(sizeof(node));

// error check
if (newNode == NULL)
{
return false;
}

// set value member of node to current word
newNode->dword = wordIn;

// first insertion into linked list if that root node has not been used yet
if(root[hash] == NULL)
{
// sets to NULL
newNode->next = root[hash];

// link it
root[hash] = newNode;
}

else if(root[hash] != NULL)
{
// starts at the root
node* current = root[hash];

// insert into new beginning of list
newNode->next = current;
root[hash] = newNode;
}
}

fclose(newDict);
return true;
}

/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
return wordCounter;
}

/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
// TODO
return false;
}

最佳答案

问题的根源是这条线:

newNode->dword = wordIn;

wordInload 中的局部数组。您正在将 wordIn 的地址存储在节点的 dword 中。当您从 load 返回时,这些地址不再有效。

你需要做的是为wordIn中的字符串分配内存,将分配的内存分配给newNode->dword,并复制wordIn<中的内容newNode->dword

如果你的平台提供了非标准函数strdup,你可以将上面这行改成:

newNode->dword = strdup(wordIn);

如果没有,很容易实现:

char* strdup(char const* in)
{
char* r = malloc(strlen(in)+1);
strcpy(r, in);
return r;
}

关于c - 链表 - C - 从节点指针数组正确访问结构节点的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23003444/

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