I am writing a code for a hangman simulation and When using strncmp I know that I'm comparing two const char* but when I set the max num to compare lets say len. when I try to compile it reads out error expected primary expression before 'len'. I even changed len to size_t to make it unsigned. still doesn't compile, any suggestion.
here is my code in this function
int processGuess(char* word, const char* targetWord, char guess)
{
char* newWord = 0;
int len = strlen(targetWord); //since targetWord is the word being guessed
strncpy(newWord,targetWord,len +1); //since targetWord is a const char* I need
// to copy it to a char*
for(int i = 0; i <len + 1; i++)
{
if (strncmp ( guess, newWord, size_t len) == 0)
{
word[i] = guess; // guess is the char user typed in
} // word[] is a blank array to place the guessed char if
// if correct
else
numTurns--; // user lost a turn if guessed wrong
}
return numTurns;
}
You're not calling strncmp right.
The compiler sees this as you trying to declare a variable inside a function call, when you're really just trying to pass in the (existing) len variable.
Get rid of the size_t and see if that fixes it for you.
...Actually, it won't, because you never allocate any space for newWord, so your strncpy call will try to copy a string to some random piece of memory that's not yours to mess with.