Your for
loop condition is wrong. You want to iterate the array of pointers to char.
Your loop for (int x = 0; x < sizeof(szWords); x++)
continues while x < sizeof(szWords)
. But sizeof(szWords)
is not array length. It just says how many bytes your array occupies in memory. It is system dependant, however it is twice the size of pointer to char, so probably 8 or 16 bytes. You need to divide this size by size of the array element then you will get the proper array size.
Rewrite your for
loop like this:
for (int x = 0; x < sizeof(szWords)/sizeof(szWords[0]); x++)
or if your compiler supports C++11 you can try range-based for:
for (const char *word : szWords)
Apart from that, if you are writing C++ code you really should use STL and other C++ features. For instance your array of strings should be declared as:
std::vector<std::string> words = { "caralho", "porra" };
or if your compiler doesnt support C++11 (then really change it...)
std::vector<std::string> words;
words.push_back("caralho");
words.push_back("porra");
for (std::size_t i = 0; i < words.size(); ++i) {
// since you are using C string functions you will need word as C string
const char *word = words[i].c_str();
// do whatever you want with word
}
Also consider reading modern C++ book before writing code.