I'm looking to try and pass a char array from one function to another. The array seems to pass into the second function, but the contents of the array seem to be lost, apart from the first element? To get round this I tried duplicating the content of the passed array into a new local array in the called function, but this then opened up the issue of passing the new array back to the original calling function and assigning it's value back to the originally passed in array.
It's likely my pointers could be completely wrong, I'm still getting to grips with pointers in C so have been following tips from the IDE until it stops throwing errors.
Here is how I'm calling the function:
wordCheck(charIn, randWord, currentWordStatus);
The array to be updated/modified is the currrentWordStatus array.
This is the content of the array when it is passed into the function:

And here is how it actually ends up in the function:

Here is the code of my called function:
void wordCheck(char guess, char *wordToGuess, char *currentWordStatus) {
int wordLen = strlen(wordToGuess);
char wordArr[wordLen];
strcpy(&wordArr[0], wordToGuess);
bool found = false;
for (int i = 0; i < wordLen; i ++) {
printf("%c \n", (char)wordArr[i]);
if ((char) wordArr[i] == guess) {
found = true;
currentWordStatus[i] = guess;
}
}
}
I was wondering what I'm doing wrong, I'm hoping to modify the function directly as it is passed 'By Reference' (I'm aware it's a pointer to the start of the array), instead of having to remake the array inside of the function and passing it back.
Hopefully that makes some sort of sense?
Many Thanks
char wordArr[wordLen];should bechar wordArr[wordLen + 1];because you also need room for the null terminator character at the end of the copied string.&wordArr[0]is exactly equal towordArr. Using the array name when a pointer is expected will decay the array to a pointer to its first element.wordToGuessstring towordArr[]at all. Just usewordToGuessdirectly.