C++ passing an array pointer as a function argument
Solution 1
You're over-complicating it - it just needs to be:
void generateArray(int *a, int si)
{
for (int j = 0; j < si; j++)
a[j] = rand() % 9;
}
int main()
{
const int size=5;
int a[size];
generateArray(a, size);
return 0;
}
When you pass an array as a parameter to a function it decays to a pointer to the first element of the array. So there is normally never a need to pass a pointer to an array.
Solution 2
int *a[]
, when used as a function parameter (but not in normal declarations), is a pointer to a pointer, not a pointer to an array (in normal declarations, it is an array of pointers). A pointer to an array looks like this:
int (*aptr)[N]
Where N
is a particular positive integer (not a variable).
If you make your function a template, you can do it and you don't even need to pass the size of the array (because it is automatically deduced):
template<size_t SZ>
void generateArray(int (*aptr)[SZ])
{
for (size_t i=0; i<SZ; ++i)
(*aptr)[i] = rand() % 9;
}
int main()
{
int a[5];
generateArray(&a);
}
You could also take a reference:
template<size_t SZ>
void generateArray(int (&arr)[SZ])
{
for (size_t i=0; i<SZ; ++i)
arr[i] = rand() % 9;
}
int main()
{
int a[5];
generateArray(a);
}
Solution 3
You do not need to take a pointer to the array in order to pass it to an array-generating function, because arrays already decay to pointers when you pass them to functions. Simply make the parameter int a[]
, and use it as a regular array inside the function, the changes will be made to the array that you have passed in.
void generateArray(int a[], int si) {
srand(time(0));
for (int j=0;j<*si;j++)
a[j]=(0+rand()%9);
}
int main(){
const int size=5;
int a[size];
generateArray(a, size);
return 0;
}
As a side note, you do not need to pass the size by pointer, because you are not changing it inside the function. Moreover, it is not a good idea to pass a pointer to constant to a parameter that expects a pointer to non-constant.
Ortharios
Updated on July 09, 2022Comments
-
Ortharios almost 2 years
I'm trying to use pointers of arrays to use as arguments for a function which generates an array.
void generateArray(int *a[], int *si){ srand(time(0)); for (int j=0;j<*si;j++) *a[j]=(0+rand()%9); } //end generateArray; int main() { const int size=5; int a[size]; generateArray(&a, &size); return 0; } //end main
But when I compile this this message appears:
cannot convert `int (*)[5]' to `int**' for argument `1' to `void generateArray(int**, int*)'
-
djechlin almost 12 yearsThere's a few more issues worth pointing out to you - one, don't pass a const int by reference, pass it by value. Two, call:
generateArray(a, sizeof(a)/sizeof(a[0]))
. Verbose but this is standard best practice when working with arrays. -
moooeeeep almost 12 yearsIf this is C++ you'd better use
std::vector
orstd::array
: They still know their size when passed to a function. -
ShadowRanger over 8 years@moooeeeep: As long as you use
std::vector<int>&
orstd::array<int>&
as the argument (or more likely, definegenerateArray
as a templated function and useT&
as the argument type); if you just do a straight swap fromint a[]
tostd::array<int, 5>
(or templatedT
used withstd::array<int, 5>
), you'll pass by value (copying stuff you didn't want to copy, operating on the copy, and leavinga
inmain
unmodified). -
moooeeeep over 8 years@ShadowRanger Which is actually another benefit, as you clearly see in the function arguments if the array elements are to be modified by the function or not.
-
-
Konrad Rudolph almost 12 yearsIMHO it’s just misleading to declare a parameter as
x[]
. Why not make it clear that it’s a pointer? -
Sergey Kalinichenko almost 12 years@KonradRudolph I agree, using a pointer is a "more honest" syntax. But since the OP used the empty square bracket syntax, I decided to stay with it.
-
Ortharios almost 12 yearsI know it is over-complicating the whole code, but the objective of this was using pointers
-
Benjamin Lindley almost 12 years@Ortharios: This is using pointers. Because through an error in language design,
int a[]
, when used as a function parameter, is actuallyint* a
. You'll see Konrad, below dashblinkenlight's answer, recommending against the confusing syntax, and I whole-heartedly agree. -
David Rodríguez - dribeas almost 12 years+1, but note that the template solution has the negative side effect of generating one function per array size that is used there.
-
Paul R almost 12 yearsIf it makes you happier I've changed the declaration for
generateArray
so that it now takes an explicit pointer. It's still the same function signature so this is just a cosmetic change, but hopefully it now satisfies the "uses pointers" requirement ? -
Super Cat over 8 yearsIn my opinion, explicitly declaring it as a pointer makes the meaning of the code less clear - In one case you know for sure that you're working with an array, and in the other you're simply told it's a pointer which you need to remember is an array. I'd revert the answer back to it's original solution just for that purpose.
-
Paul R over 8 years@SuperCat: I agree - code should express intent as far as possible, and a pointer is always somewhat ambiguous. However this seems to have been some sort of lame homework assignment where the objective is to do as you are told rather than do what's best.