0

I would like to know - is there any way to copy array to a function in C/C++? Not pass it just an address, but create a copy... Like for int value. For example here we send not a copy(((:

void arrayAbs(int array[], int size) {
    for ( int i = 0; i < size; i++ ) {
        if ( array[i] < 0 ) {
            array[i] *= -1;
        }
    }
}
2
  • 4
    Use std::array<T> (see reference), or std::vector<T> Commented Sep 11, 2014 at 19:25
  • You'll need to write a function to make a copy of your raw array. Otherwise use a std::vector or (better) std::array (C++11 only) Commented Sep 11, 2014 at 19:26

4 Answers 4

1

This is not possible for arrays - at least when the size of the array is not known at compile time. Since you are passing size as a separate parameter, it appears that a solution with a fixed-size array would not work in your situation.

However, you can use std::vector<int> instead. This container will be copied when you pass it by value. As an additional benefit, it knows its size, so passing int size would become unnecessary.

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

Comments

1

C and C++ arrays are not copyable. But you can use an array-like standard library container type, such as std::array:

template <size_t N>
void arrayAbs(std::array<int, N> a) 
{
  for ( int i = 0; i < a.size(); i++ ) {
        if ( a[i] < 0 ) {
            a[i] *= -1;
        }
    }
}

Here, a is passed by value, so it is a copy of the array passed in by the caller.

Comments

0

You can not do this with arrays. Either arrays are converted to pointers to their first elements when passed to a function (in C and C++) or if the parameter is defined as a reference to an array then the reference is passed. in any case neither copy of an array is created.

In C++ you could use standard class std::array. in this case you indeed could work with a copy of the original array. In C you could use an analogy of std::array in C++ by wrapping an array in a structure.

4 Comments

Note that OP passes array size as a separate parameter. The implication would be that his arrayAbs would become generic on the size of the std::array passed into it (see Juan's answer).
@dasblinkenlight it is only details.
Perhaps that's "only details" to someone with years of experience in C++ and deep knowledge of the Standard C++ Library. However, to a reader who wants to find out if it's possible to pass arrays by value this would not be obvious at all.
@dasblinkenlight Class std:;array is itself a template class. So this details are simply .useless.
0

In C++: Use std::array. In C: Wrap it in a struct.


typedef struct {
    int array[10];
} wrapper;

wrapper function(wrapper wrapped_array) {
    wrapped_array.array[0] = 42;
    return wrapped_array;
}

int main() {
    wrapper wa;
    wrapper wa2 = function(wa);
}

Comments

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.