Hi for some reason Strcat does not like the value property within my structure. 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);
}
}
}
}
Proof that the value from the structure returns something (the 'l' character)
I've been trying to get strcat
working for hours and I can't get it to work even reading the online tutorials. All help appreciated.
The strcat_s
function expects a char *
, specifically a pointer to a null terminated string, as the third argument. You're passing in a single 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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.