Can You Pass an Array by Reference in C++
A whole array cannot be passed as an statement to a role in C++. You tin, however, pass a pointer to an array without an index by specifying the array's name.
In C, when we laissez passer an array to a role say fun(), it is always treated as a pointer by fun(). The below example demonstrates the same.
C++
#include <iostream>
using
namespace
std;
void
fun(
int
arr[])
{
unsigned
int
northward =
sizeof
(arr) /
sizeof
(arr[0]);
cout <<
"\nArray size inside fun() is "
<< n;
}
int
main()
{
int
arr[] = { 1, 2, iii, 4, 5, 6, vii, eight };
unsigned
int
n =
sizeof
(arr) /
sizeof
(arr[0]);
cout <<
"Array size inside main() is "
<< n;
fun(arr);
render
0;
}
C
#include <stdio.h>
#include <stdlib.h>
void
fun(
int
arr[])
{
unsigned
int
north =
sizeof
(arr)/
sizeof
(arr[0]);
printf
(
"\nArray size within fun() is %d"
, due north);
}
int
principal()
{
int
arr[] = {1, two, 3, 4, v, 6, 7, eight};
unsigned
int
n =
sizeof
(arr)/
sizeof
(arr[0]);
printf
(
"Array size inside main() is %d"
, n);
fun(arr);
return
0;
}
Output
Array size within master() is eight Array size inside fun() is 1
Therefore in C, nosotros must laissez passer the size of the assortment equally a parameter. Size may not be needed only in the instance of '\0' terminated character arrays, size can be determined by checking the end of string character.
Following is a uncomplicated example to show how arrays are typically passed in C:
C++
#include <iostream>
using
namespace
std;
void
fun(
int
*arr, unsigned
int
n)
{
int
i;
for
(i = 0; i < n; i++)
cout <<
" "
<< arr[i];
}
int
master()
{
int
arr[] = {i, 2, 3, 4, 5, 6, vii, viii};
unsigned
int
n =
sizeof
(arr)/
sizeof
(arr[0]);
fun(arr, north);
render
0;
}
C
#include <stdio.h>
void
fun(
int
*arr, unsigned
int
due north)
{
int
i;
for
(i=0; i<northward; i++)
printf
(
"%d "
, arr[i]);
}
int
main()
{
int
arr[] = {1, 2, 3, four, v, 6, 7, 8};
unsigned
int
due north =
sizeof
(arr)/
sizeof
(arr[0]);
fun(arr, northward);
return
0;
}
Output
1 2 3 iv five 6 7 8
Do: Predict the output of the below C programs:
Programme 1:
C++
#include <iostream>
using
namespace
std;
void
fun(
int
arr[], unsigned
int
n)
{
int
i;
for
(i = 0; i < n; i++)
cout << arr[i] <<
" "
;
}
int
chief()
{
int
arr[] = { 1, 2, 3, four, v, six, 7, eight };
unsigned
int
n =
sizeof
(arr) /
sizeof
(arr[0]);
fun(arr, northward);
render
0;
}
C
#include <stdio.h>
void
fun(
int
arr[], unsigned
int
north)
{
int
i;
for
(i=0; i<n; i++)
printf
(
"%d "
, arr[i]);
}
int
main()
{
int
arr[] = {1, ii, 3, 4, v, 6, vii, 8};
unsigned
int
n =
sizeof
(arr)/
sizeof
(arr[0]);
fun(arr, n);
return
0;
}
Output
1 2 3 4 5 6 7 viii
Programme 2:
C++
#include <iostream>
using
namespace
std;
void
fun(
int
* arr)
{
int
i;
unsigned
int
n =
sizeof
(arr) /
sizeof
(arr[0]);
for
(i = 0; i < n; i++)
cout <<
" "
<< arr[i];
}
int
main()
{
int
arr[] = { 1, 2, 3, 4, 5, 6, vii, 8 };
fun(arr);
return
0;
}
C
#include <stdio.h>
void
fun(
int
*arr)
{
int
i;
unsigned
int
north =
sizeof
(arr)/
sizeof
(arr[0]);
for
(i=0; i<n; i++)
printf
(
"%d "
, arr[i]);
}
int
main()
{
int
arr[] = {1, two, three, 4, v, vi, vii, viii};
fun(arr);
return
0;
}
Output
1 2
Program three:
C++
#include <iostream>
#include <string.h>
using
namespace
std;
void
fun(
char
* arr)
{
int
i;
unsigned
int
due north =
strlen
(arr);
cout <<
"n = "
<< northward << endl;
for
(i = 0; i < n; i++)
cout << arr[i] <<
" "
;
}
int
main()
{
char
arr[] =
"geeksquiz"
;
fun(arr);
render
0;
}
C
#include <stdio.h>
#include <string.h>
void
fun(
char
*arr)
{
int
i;
unsigned
int
n =
strlen
(arr);
printf
(
"north = %d\n"
, north);
for
(i=0; i<n; i++)
printf
(
"%c "
, arr[i]);
}
int
main()
{
char
arr[] =
"geeksquiz"
;
fun(arr);
return
0;
}
Output
n = 9 chiliad east due east k s q u i z
Programme 4:
C++
#include <$.25/stdc++.h>
#include <iostream>
using
namespace
std;
void
fun(
char
* arr)
{
int
i;
unsigned
int
n =
strlen
(arr);
cout <<
"north = "
<< n <<
"\n"
;
for
(i = 0; i < n; i++)
cout <<
" "
<< arr[i];
}
int
principal()
{
char
arr[]
= {
'1000'
,
'e'
,
'eastward'
,
'1000'
,
'due south'
,
'q'
,
'u'
,
'i'
,
'z'
};
fun(arr);
return
0;
}
C
#include <stdio.h>
#include <string.h>
void
fun(
char
*arr)
{
int
i;
unsigned
int
n =
strlen
(arr);
printf
(
"north = %d\due north"
, n);
for
(i=0; i<n; i++)
printf
(
"%c "
, arr[i]);
}
int
principal()
{
char
arr[] = {
'g'
,
'e'
,
'eastward'
,
'thou'
,
's'
,
'q'
,
'u'
,
'i'
,
'z'
};
fun(arr);
return
0;
}
Output
n = 11 g east e k s q u i z
NOTE: The character array in the above plan is non '\0' terminated. (See this for details)
Now These were some of the common approaches that nosotros utilize just practice you know that there is a ameliorate manner to do the aforementioned. For this, we first need to await at the drawbacks of all the to a higher place-suggested methods:
Drawbacks:
- A major drawback of the above method is compiler has no idea about what y'all are passing. What I hateful here is for compiler we are only passing an int* and we know that this is pointing to the assortment simply the compiler doesn't know this.
- To verify my statement you tin can phone call for-each loop on your assortment. You will surely become an mistake saying no callable begin, cease role constitute.
This is because the passing array is like actually passing an integer pointer and information technology itself has no information most the underlying array hence no iterator is provided.
Template Approach (Reference to Array):
This method retains all information most the underlying assortment. This method is majorly based on reference to an array but using this with templates optimizes our method. Template dependency actually calculates the length of the array automatically at the time of function call and so that it can be used to create a reference considering a reference to an array must know the size of the array.
Here Template is used for template argument deduction.
C++
#include <iostream>
using
namespace
std;
template
<
size_t
N>
void
print(
int
(&a)[N])
{
for
(
int
eastward : a) {
cout << east << endl;
}
}
int
chief()
{
int
a[]{ 1, 2, iii, 4, 5 };
print(a);
}
Here you lot can see why we need template argument deduction. For a base to create a reference to an array and then that we can take an array as a parameter.
Related Articles:
- Do not utilize sizeof for array parameters.
- Difference between pointer and array in C?
Please write comments if you find annihilation incorrect, or you want to share more than information virtually the topic discussed hither.
barthrebrispere1986.blogspot.com
Source: https://www.geeksforgeeks.org/how-arrays-are-passed-to-functions-in-cc/
0 Response to "Can You Pass an Array by Reference in C++"
Post a Comment