You need an array as an input parameter.
void f (int & amp; heap [])
{
}
The compiler swears at the code above.
Answer 1, authority 100%
Hopefully this is clearer now.
# include & lt; stdio.h & gt;
void
elem2 (int a [], int v)
{
a [2] = v * v;
}
int
main ()
{
int a [3] = {1,2,3};
elem2 (a, 10);
return printf ("elem [2] =% d \ n", a [2]) == EOF;
}
c: / Documents and Settings / avp / src / hashcode $ gcc a.c
c: / Documents and Settings / avp / src / hashcode $ ./a
elem [2] = 100
c: / Documents and Settings / avp / src / hashcode $ echo $?
0
c: / Documents and Settings / avp / src / hashcode $
Answer 2, authority 96%
The correct syntax for an array reference is
void f (int (& amp; arr) [123]) {}
If the size of the array is unknown, then you can use the template:
template & lt; std :: size_t N & gt; void f (int (& amp; arr) [N]) {}
If what is passed to the function is not an array, but a pointer (that is, not T [N]
a T *
), then the function must accept a pointer , and the size must be passed as an additional parameter or something else.
void f (int * ptr, std :: size_t n) {}
However, this is a very unreliable solution, there is no guarantee that the correct size of the array will be passed, or that ptr
is not nullptr
.
Therefore, it is correct to use span & lt; T & gt;
from C++ Core Guidelines
void f (gsl :: span & lt; T & gt; arr) {}
Also, instead of the T [N]
array, you can pass a link to std :: array
void f (std :: array & lt; T, N & gt; & amp; v) {}
And instead of a dynamic array, use std :: vector
:
void f (std :: vector & lt; T & gt; & amp; v) {}
-- or --
void f (const std :: vector & lt; T & gt; & amp; v) {}
Answer 3, authority 88%
Try to pass a pointer to an array, if it is one-dimensional, then:
void f (int * arr) {}
Answer 4, authority 75%
Two ways to pass an array to a function:
void foo (int * arr) {
....
}
or
void foo (int arr []) {
.....
}
But with both methods, as indicated, it makes sense to pass the size of the array as the second (or other) parameter, or somehow fix its end in the array itself.