简体   繁体   English

连接时出现 Strcat 问题

[英]Strcat issues when concatenating

Hi for some reason Strcat does not like the value property within my structure.嗨,出于某种原因,Strcat 不喜欢我的结构中的 value 属性。 I'm not sure why.我不知道为什么。 Here is my structure code:这是我的结构代码:

typedef struct TrieSearchTree{
char value;
struct DynamicList* children; 
};

and here is my method:这是我的方法:

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)证明结构中的值返回了一些东西('l' 字符)

I've been trying to get strcat working for hours and I can't get it to work even reading the online tutorials.我一直试图让strcat工作几个小时,即使阅读在线教程也无法让它工作。 All help appreciated.所有帮助表示赞赏。

The strcat_s function expects a char * , specifically a pointer to a null terminated string, as the third argument. strcat_s函数需要一个char * ,特别是一个指向空终止字符串的指针,作为第三个参数。 You're passing in a single char .您传入的是单个char Your compiler should have warned you about this.你的编译器应该已经警告过你了。

That character is being interpreted as a pointer and being dereferenced.该字符被解释为指针并被取消引用。 This invokes undefined behavior , which in this case manifests in a crash.这会调用未定义的行为,在这种情况下,它表现为崩溃。

If you want to append a single character to a string, you need to add it and a new null terminator manually.如果要将单个字符附加到字符串,则需要手动添加它和一个新的空终止符。

char c[CHAR_LENGTH] = "";
strcat_s(c, CHAR_LENGTH, word);
c[strlen(c) + 1] = '\0';
c[strlen(c)] = trieSearchTree->value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM