Im currently running into an issue where I have to input words (strings) into a binary search tree and am doing so by putting words(strings) into string arrays, however when I try to put it into the first element, it is segmentation faulting.
Here is what I have:
node.h
typedef struct Node{
char letter;
int asiccValue;
struct Node *left, *right;
string words[99];
int wordCount;
}Node;
tree.cpp
// This function creates new nodes as needed
Node *createNode(string word){
// Assigns values
struct Node *temp = (Node*)malloc(sizeof(Node));
temp->letter = word[0];
temp->asiccValue = (int)word[0];
temp->left = NULL;
temp->right = NULL;
temp->words[0] = word;
temp->wordCount = 1;
return temp;
}
stringis actuallystd::stringand this question is for the C++ programming language the answer will be radically different that it will be ifstringis atypedefand this is a C question. The crux would be under no circumstances should youmallocstorage forstd::stringunless you're already an expert in C++ and would not need to ask this question.mallocingstrings requires special handling (search keyterm: placementnew) becausemalloconly provides storage. It does not call constructors, and an unconstructedstringis a timebomb..vectoris unnecessary.struct Node *temp = (Node*)malloc(sizeof(Node));doesn't know how to construct the 99strings in thewordmember ofNode(see comment above). You can solve this withstruct Node *temp = new Node;newallocates storage for and then constructs aNode.