This article was written by Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions.
When you pass an array to a function in C++, the address of the first element is passed rather than a copy of the entire array. You can pass an array using Call By Reference, which impacts the array's original values, or using Call By Value, which creates a new copy of the array rather than modifying the original. This wikiHow article will teach you two ways to pass arrays to functions in C++.
Steps
Method 1
Method 1 of 2:Pass the Array by Pointer
-
1Passing by pointer allows the array's values to be changed by the function. When you pass an array to a function, you're passing a pointer to the array's first element.[1] In this example, we'll pass the entire array to a function called show. The pointer we'll use ptr will store the array's bass address, which is the first element of the array. The output will print all elements in the array.
#include <iostream> using namespace std; void show(int *arr, int n) { //print all elements in the array for(int i=0; i<n; i++) { cout<<"arr["<<i<<"] = "<<*(ptr+i)<<endl; } } int main() } //initializing the array int arr[10] = {1,2,3,4,5,6,7,8,9,10}; //10 is the number of elements show( arr, 10); //passing array to function return 0; }
- Because the pointer doesn't contain size information (which is called pointer decay), you'll need to specify the number of elements in the array in a separate parameter.
- If you don't want the function to modify the object, add const to the pointer parameter.[2]
- If passing a 2-dimensional array, you must include a pair of empty brackets, then another pair of brackets that includes the number of columns in the array.
Advertisement
Method 2
Method 2 of 2:Pass an Array by Value
-
1Wrap the array inside of a structure to pass as a value. In contrast to passing with call by reference, passing with call by value ensures that changes made to the main function's parameter will not affect the original values.[3] Although C++ arrays cannot typically be passed using call by value, you can wrap the array in a structure and assign values to the array using an object of the structure. The structure can then be passed to a function. In this example, we'll assign values to an array that's declared inside of a structure called sample using the object of that structure. Then, we'll pass use the structure as the return type to print the contents of the array.
#include <iostream> using namespace std; struct sample { //this declares the array inside the structure "sample" int arr[100]; }; struct sample func(int n) //return type is struct { struct sample sample_mem; //sample structure member now declared for(int i=0;i<n;i++) { //array initializing sample_mem.arr[i] = i; } return sample_mem; //returning address of structure } int main() { struct sample a; int n=5; //count of elements a=func(n); //address of array cout<<"This Array is : "; for(int i=0;i<n;i++) { cout<<a.arr[i]<<"\t"; } return 0; }
-Step-16-Version-3.webp)













-Step-16-Version-3.webp)


