c - 连接时出现 Strcat 问题

标签 c visual-studio

嗨,出于某种原因,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);
            }
        }
    }
}

Here is the error message:

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/

相关文章:

visual-studio - "Everyone"或 "Just me"是 Windows Installer 程序包的默认选项吗?

c++ - Visual Studio 外部依赖项符号错误

regex - 在Visual Studio中使用正则表达式(regex)查找和替换?

javascript - 为 VS2017 设置 TSLint

c - 在 MPI 中实现 Cannons 算法

c - 冒泡排序到 char 指针

c - 在 R 中抑制来自底层 C 函数的消息

c - fputs() 在 MacOSX 上的行为不正常

c - 让这个计数器更好地工作

c# - 如何使用 VS2008 将应用程序 list 嵌入到应用程序中?