0

I have been looking at other posts and trying to get this working for a bit, but can't seem to manage it.

Basically I want to pass a "char myArray[10]" though into a function, have the function assign the values and then hand it back. It generally looks like this at the moment:

int MyClass::GetArray(char array[10])
{   
    char p[10];
    ... // a value is assigned to p
    memcpy(&array, &p, sizeof(p)); // Here array ends up being 0x3232323232323232 <Error reading characters of string.>

    return 0;
}

Called with:

    char array[10];
    myclass.GetArray(array);

So, I assume I need to pass the array through as a reference to the array[10] created before calling the function. But for that I am unsure how to create a pointer to a fixed array without making it either a general char* pointer or a pointer to an array of chars.

Secondly is the memcpy error (in the code comments above). I'm not sure if that is related or not though.

2
  • 2
    Use std::array in place of built-in arrays. It has a size function (you don't know the size of the parameter in your example) and it supports assignment. Commented Jul 12, 2014 at 21:14
  • 1
    In memcpy, pass in array instead of &array. It'll decay to a pointer to the first element. Same for p. Why are you copying the memory anyway? (Actually, it could only (easily) make sense if you were passing a reference to the array, but that's what you're asking about, so nevermind~) Commented Jul 12, 2014 at 21:14

1 Answer 1

1

Then thing is that when you pass an array to a function, it decays to a pointer. So when you use the address-of operator & on array in the function, you're taking the address of the pointer, meaning you get a pointer to a pointer.

That, by the way, leads to undefined behavior.

Other than that it's all okay, you don't have to pass the array (or rather, pointer) by reference. It's just not very... C++-ish. :)

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

3 Comments

Okie. Removing the refernce(&) as you and Mohammad suggested works perfectly. But it sounds like you might have a better suggestion on how to do this? :) @chris Yeah, I didn't realise that the compiler ignored that value. For my program though everything will be 10 bytes and the char array[10] will be the only array passed through :)
@user1539405 I would suggest using std::array even if it's the only array you use. The formal parameter to the function would then need to passed by reference, e.g. std::array<char, 10>& array. Using a standard contain like std::array would keep you from making mistakes like (old C-style) arrays decaying to pointers.
All right, will update it to an std array. And report back when I somehow screw it up ;)

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.