Stack Exchange
Why not use the root of the trie?
I'm trying to understand this program, why do they have to make Trie* pCrawl = root, why don't they just use the root of the tree and insert directly to it:
void insert(struct TrieNode *root, string key)
{
    struct TrieNode *pCrawl = root;
  
    for (int i = 0; i < key.length(); i++)
    {
        int index = key[i] - 'a';
        if (!pCrawl->children[index])
            pCrawl->children[index] = getNode();
  
        pCrawl = pCrawl->children[index];
    }
  
    // mark last node as leaf
    pCrawl->isEndOfWord = true;
}


Where do you have found this bad code? The function can invoke undefined behavior when root is equal to NULL.

Generally speaking, a variable named root should never represent something other than the root of the data structure imo. So the author copied it to a local variable for iteration purposes.

void insert(struct TrieNode *root, string key) -- Yet another horrid code example, where the coder can't make up their mind if they are writing C++ or C code.  The dead giveaway is the usage of struct, when in C++, there is no need to specify it there.

Bonus points for using string but the rest is what C programmers write. Drop all the struct. Use ranged-for and new TrieNode().

It is just a good practice to not modify function augment. Some tools even enforce it. For compiler from final code point of view there is no difference (as-if rule).

And root should be TrieNode &root because passing nullptr would cause UB and is never needed. The reference would encode that fact in the signature leading to a safer interface. const string &key for performance reasons. Note: with &root the pCrawl thing makes sense again.

I don't think pCrawl is needed.
The argument root is a copy of what is passed and modifying that won't affect the caller, so it is free to modify the value of root in the function.
It may be for clarity gained by using clear names for the argument (for input) and variable (for processing).

@GoswinvonBrederlow - U wot m8? "Broken" code is something that is guaranteed to crash and burn, this here is simply a stylistic choice (that is very debatable, despite the fact some developers think there's their opinion and the wrong opinion).
