Are you new to C++ programming and looking for solutions to pass arrays to functions by reference in C++ (CPP)? Congratulations 🥳, you are at the right place. Keep Reading. 📖
An array is an easy and widely used data structure in programming languages. It is a consecutive memory location as it is a very widely used structure; that’s why it is essential to know how to pass an array to a function exactly. In this writing, we will discuss different ways to send an array by reference.
Before starting the article, we should know about arrays and whether an array name is a pointer. So without further delay, start our writing.
Table of Contents
What is an Array in C++?
An array is a widely used data structure that can store multiple values of the same data type in a single variable. To declare an array, we tell its data type and the number of elements enclosed in square brackets.
Its general syntax is:
datatype arrayname[size of an array];
An array is a derived data type composed of primitive data types. An array can store more than one value at a time.
For example:
int myarray[10];
Here we declare an array of 10 elements, and their data type is int.
Is the Array Name a Pointer?
In C++, an array’s name is considered a pointer, meaning an array name contains the address of the first element.
For example, if we declare an array of “rollno” which contains 10 elements, in this example, “rollno” is a pointer containing the address of the first element of an array; now understand this example with the help of coding:
Code
#include <iostream> using namespace std; int main() { // integer pointer declaration int *ptr; // roll no array declaration and initialization int rollno[4]={1,2,3,4}; // assign address of array rollno first element to pointer ptr ptr=rollno; std::cout << "The value of *ptr is :" <<*ptr<< std::endl; std::cout << "The value of *rollno is :" <<*rollno<<std::endl; // Access the next element by just adding 1 in the pointer; std::cout << "The value of *ptr + 1 is :" <<*rollno + 1<< std::endl; }
Output
The value of *ptr is :1 The value of *rollno is :1 The value of *ptr + 1 is :2
Can We Pass an Array by Value to A Function?
No, passing an array to a function by value is impossible. In C++, we cannot pass the whole array to a function; we can pass the name of an array without an index.
Let’s understand this concept with the help of an example:
Code
#include <iostream> using namespace std; void printArray(int newarray[], int n) { int i; for (i = 0; i < n; i++) cout << newarray[i] <<endl; } int main() { int myarray[] = {1, 2, 3, 4, 5}; // passing the complete array to a function is not possible // we can only pass the address of the first element printArray(myarray[], 5); return 0; }
Output
How to Pass Arrays by Reference to a Function in C++?
Remember, C++ does not allow sending a complete array as an argument to a function. We can pass an array by reference to a function. We can understand this concept with the help of the following example:
Code
#include <iostream> using namespace std; // newarray is just like a pointer // just like int *newarray void printArray(int newarray[], int n) { int i; for (i = 0; i < n; i++) cout << newarray[i] <<endl; } int main() { int myarray[] = {1, 2, 3, 4, 5}; // passing address of first element and length of an array printArray(myarray, 5); return 0; }
Output
1 2 3 4 5
In the above example, we can also write the printArray function with a pointer. e.g.
void printArray(int *newarray, int n)
Both statements have the same meaning: an array can be passed by reference only; in this example, we pass the address of the first element to a pointer, and then we can perform operations on an array. Whenever we pass an array by reference, we have a problem that we have to pass the array length to perform different operations on an array. If we don’t want to send the size of an array, then we can use templates.
Our function keeps all information about the underlying array in the template approach. By using a template, we optimize our method. At the time of the function call template automatically calculates the array’s size. Let’s understand the template with the help of an example:
Code
// Example to demonstrate the template approach #include <iostream> using namespace std; template <size_t no> void printArray(int (&array)[no]) { // Use range base for loop for (int value : array) { cout << value << endl; } } // Main function int main() { int myarray[]={ 1, 2, 3, 4, 5 }; // Passing myarray reference to function printArray printArray(myarray); }
Output
1 2 3 4 5
Conclusion
So far, we have discussed “Passing arrays by reference to functions in C++” and concluded that we could pass an array by reference in C++. It is different from passing a simple variable to a function by reference. In the case of an array, we write the name of an array that contains the first element’s address, so whether we write array[ ] or pointer in the function definition, both work for us to handle the reference of an array.
Finally, remember, in C++, we can not pass an entire array to a method. However, we can pass the address of the first element of an array(reference) as an argument to the method by writing the array’s name without any index.
Here is a quick recap of the topics discussed in the article:
- What is an array?
- Is an array name a pointer?
- Can we pass an array by value in C++?
- How to pass an array by reference in C++?
If you find this article helpful, share it with your friends 👫and let us know in the comment section below if we can pass an array by value or reference in C++.
Happy Learning!