Pass an array by value to a function in C/C++
This post will discuss how to pass an array by value to a function in C/C++.
We know that arguments to the function are passed by value in C by default. However, arrays in C cannot be passed by value to a function, and we can modify the contents of the array from within the callee function. This is because the array is not passed to the function, but a copy of the pointer to its memory address is passed instead. So, when we pass an array in a function, it would decay into a pointer regardless of whether the parameter is declared as int[] or not.
However, few tricks do allow us to pass an array by value in C/C++.
1. Using structures in C
The idea is to wrap the array in a composite data type such as structures in C. This works since structs are passed by value to a function and won’t decay to pointers. In C++, we can use class.
This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> #define N 5 struct Array { size_t size; int arr[N]; }; void increment(struct Array array) { for (int i = 0; i < array.size; i++) { array.arr[i]++; } } int main(void) { struct Array array = { N, { 1, 2, 3, 4, 5 } }; increment(array); for (int i = 0; i < array.size; i++) { printf("%3d", array.arr[i]); } return 0; } |
Output:
1 2 3 4 5
This only works when the struct contains an actual array and not a pointer to an array. This is because in the case of pointers, the address of the original array will be referenced, and any change to array contents will be visible both within and outside the function.
2. Create a copy
Another solution is to create a copy of the array inside the callee function and use the copy to perform any modifications. Alternatively, we can also pass a copy of the array to the function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> #include <string.h> void increment(int arr[], int n) { int A[n]; memcpy(A, arr, n * sizeof(int)); for (int i = 0; i < n; i++) { A[i]++; } } int main(void) { int arr[] = { 1, 2, 3, 4, 5 }; int n = sizeof(arr) / sizeof (arr[0]); increment(arr, n); for (int i = 0; i < n; i++) { printf("%2d", arr[i]); } return 0; } |
Output:
1 2 3 4 5
3. Use std::array or std::vector function
In C++, the standard approach is to use the std::array container to encapsulate fixed-size arrays. Unlike a C-style array, it doesn’t decay to a pointer automatically and can be passed to a function by value. We can also use the std::vector container in C++.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <array> template<size_t N> void increment(std::array<int, N> arr) { for (int i = 0; i < arr.size(); i++) { arr[i]++; } } int main() { std::array<int, 5> arr { 1, 2, 3, 4, 5 }; increment(arr); for (int i: arr) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5
That’s all about passing an array by value to a function in C, C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)