0

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

7
  • 1
    char wordArr[wordLen]; should be char wordArr[wordLen + 1]; because you also need room for the null terminator character at the end of the copied string. Commented Jan 9, 2020 at 16:21
  • 1
    Note that &wordArr[0] is exactly equal to wordArr. Using the array name when a pointer is expected will decay the array to a pointer to its first element. Commented Jan 9, 2020 at 16:24
  • All seems to be working now, thank you gents. Very much appreciated! Commented Jan 9, 2020 at 16:33
  • You shouldn't actually need to copy the wordToGuess string to wordArr[] at all. Just use wordToGuess directly. Commented Jan 9, 2020 at 16:34
  • @IanAbbott I wasn't aware you could index them directly, without copying them into an array. I'm still getting used to how strings are treated in C, coming from languages like Java. Have just tried, works perfectly. Thanks! Commented Jan 9, 2020 at 16:37

1 Answer 1

1

In the function all you really have is a pointer to the first element of the array. The program (or the compiler, and as you noticed not even the debugger) doesn't really know what a pointer is really pointing to inside a function.

There's nothing wrong, it's just how C and arrays and pointers in C works.

Sign up to request clarification or add additional context in comments.

2 Comments

Right okay, I see. Does this mean it isn't really possible to pass an array into the function and modify it as I'm planning to? If not, how would you suggest I go about it?
@JackPollock The code is perfectly valid, as long as you don't go out of bounds of any arrays. You can still use the pointer as an array, and currentWordStatus[i] is fine and will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.