Initializing an array of structs with functions

Hello, I need some help creating the function prototype and function call for an array of structs. I have never done this before any help appreciated. Lets use the given struct and code below as reference please. The goal is to make an array of 3 structs and use a function to initialize them. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

   struct PayInfo
      {
	int hours;        
	double payRate;   
	double grossPay;  
      };

/** Global Variables **/
const int NUM_WORKERS = 3; // number of workers
	
populateArray(struct PayInfo workers[NUM_WORKERS]);//?? prototype here,how ??
int main ()
{
	PayInfo workers[NUM_WORKERS];  // array of PayInfo structures 

         populateArray() //?? call function how ??


return 0;
}
 
void populateArray(struct PayInfo workers[NUM_WORKERS]);


1
2
3
4
5
6
7
8
int main()
{
    PayInfo workers[NUM_WORKERS];  // array of PayInfo structures 

    populateArray(workers); //?? call function how ??

    return 0;
}
Thank you that leads me in a better direction but if I need to return the array of structs it cannot be void?? My apologies for not specifying I need to return them.
Last edited on
thanks again
Last edited on
Hi @CisntEZ,

Why do you need to return an array?

I created an example to show you how you could populate an array!


1 Product name: Some Name
  Product price: 30.5

2 Product name: Some Name
  Product price: 30.5

3 Product name: Some Name
  Product price: 30.5



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>

struct Product {

    std::string name;
    double price;
    
};

const int NUM_PRODUCTS = 3;

void populateArray(Product products[], const int size);
void printArray(Product products[], const int size);

int main(int argc, const char * argv[]) {
    
    
    Product parray[NUM_PRODUCTS];
    
    populateArray(parray, NUM_PRODUCTS);
    printArray(parray, NUM_PRODUCTS);
    
        return 0;
}


void populateArray(Product products[], const int size)
{
    for(int i = 0; i < size; ++i)
    {
        products[i].name = "Some Name";
        products[i].price = 30.50;
    }
}

void printArray(Product products[], const int size)
{
    for(int i = 0; i < size; ++i)
    {
        std::cout<< i+1 <<" Product name: " << products[i].name
        <<"\n  Product price: " <<products[i].price <<"\n\n";
    }
}
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.
Last edited on
I think I got it thank you all!
Topic archived. No new replies allowed.