嗨,出于某种原因,Strcat 不喜欢我的结构中的 value 属性。我不知道为什么。这是我的结构代码:
typedef struct TrieSearchTree{
char value;
struct DynamicList* children;
};
这是我的方法:
void PrintDynamicListContents(struct DynamicList* dynamicList, char* word)
{
struct dynamicListNode* currentRecord;
struct TrieSearchTree* trieSearchTree;
struct dynamicListNode* nextRecord = dynamicList->head;
while(nextRecord != NULL)
{
currentRecord = nextRecord;
nextRecord = currentRecord->next;
trieSearchTree = currentRecord->entity;
if (trieSearchTree != NULL)
{
if (trieSearchTree->value != WORD_END_CHAR)
{
char c[CHAR_LENGTH] = "";
strcat_s(c, CHAR_LENGTH, word);
strcat_s(c, CHAR_LENGTH, trieSearchTree->value);
PrintDynamicListContents(currentRecord, c);
}
else
{
printf("%s", word);
}
}
}
}
Proof that the value from the structure returns something (the 'l' character)
我已经尝试让 strcat
工作几个小时,但即使阅读在线教程也无法让它工作。感谢所有帮助。
最佳答案
strcat_s
函数需要一个 char *
作为第三个参数,特别是指向以 null 结尾的字符串的指针。您正在传递一个char
。你的编译器应该警告你这一点。
该字符被解释为指针并被取消引用。这会调用 undefined behavior ,在本例中表现为崩溃。
如果您想将单个字符附加到字符串中,则需要手动添加它和新的空终止符。
char c[CHAR_LENGTH] = "";
strcat_s(c, CHAR_LENGTH, word);
c[strlen(c) + 1] = '\0';
c[strlen(c)] = trieSearchTree->value;
关于c - 连接时出现 Strcat 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41765523/