If I need to return the array of structs the return type cannot be void? |
It appears you're suffering some fundamental confusion about array parameters; you don't actually need to return the array in this context.
The syntax for "passing arrays" in C++ is an artifact of a misleading behavior from C. You cannot return or pass array types by value.
All three of these forms are semantically identical:
1 2 3
|
void foo(int a[integral_constant_expression]) {}
void foo(int a[]) {}
void foo(int* a) {}
|
This is highly unintuitive. The implication is that any array which appears in a function call expression undergoes an array-to-pointer conversion to a pointer to its' first element. This (and some other related conversions) is called
type decay.
That is, inside @integralfx's
void populateArray(PayInfo workers[NUM_WORKERS]);
The formal parameter
workers
binds a
PayInfo*
, pointing to the first of (hopefully)
NUM_WORKERS
PayInfo objects (but this isn't enforced).
My advice is that you never write the above syntax again, and use something like
void populateArray(PayInfo* workers, int const size);
where
size
is the number of elements pointed to by
workers
.
Critically,
the elements pointed to by workers
can be modified through that pointer. See @eyenrique's excellent post above for an example.
If you want to enforce that there are exactly NUM_WORKERS elements in the array, pass the array by reference; reference types do not decay. Only arrays decay.
void populateArray(PayInfo(&workers)[NUM_WORKERS]);
You cannot return arrays. You can instead return pointers to their first element, but be careful to not return dangling pointers.