1

My question is how do you pass a struct.variable (or the struct array) to the void function. Basically the code looks as follows:

Structs

struct Person{
    string surname;
    string BType;
    string organ;
    int age;
    int year, ID, IDp;
} Patient[50], Donor[50];

int i; // counter variables for the arrays such as Patient[i].BType... etc
int i1; 

Then the code for the function is a line like this:

void compare(int &i, int &i1, Person &Patient[50], Person &Donor[50]);

I tried to pass the i, i1, Patient and Donor structs. Why won't this work? Is there a special way to pass these sorts of structs to a function?

The values into the variable structs also are read from a file (don't think that changes anything here). Any ideas?

krsteeve
  • 1,794
  • 4
  • 19
  • 29
  • you should use vector – billz Oct 11 '13 at 03:42
  • 1
    Drop the ampersands in the function signature. There ain't no such thing as an array of references. – Igor Tandetnik Oct 11 '13 at 03:43
  • see [http://stackoverflow.com/questions/1106957/pass-array-by-reference-in-c] – Mike Godin Oct 11 '13 at 03:47
  • @MikeGodin there are safer ways to do that in C++, and array references are plausible to a specific degree. – WhozCraig Oct 11 '13 at 03:49
  • @BorysOstapienko: Like Igor Tandetnik Igor Tandetnik says either drop the `&` to make it a pointer to `Person` and obtain the size as a parameter [or] wrap `&Patient` with parenthesis like WhozCrazy suggests in his answer. – legends2k Oct 11 '13 at 05:03
  • Surprised no one has commented on the use of capitalised parameter names... Well, I have now. Its in consistent. Parameters should be called patient and doctor. In fact, the fields in the struct SHOULD be capitalised. – Sellorio Oct 11 '13 at 05:09

3 Answers3

5

Your function prototype is incorrect. To pass a fixed array type reference, you must qualify the reference part of the parameter outside the array index declaration.

void compare(int &i, int &i1, Person (&Patient)[50], Person (&Donor)[50])
//  note parens ----------------------^-------^-------------^------^

Invoke as simply

compare(i, i1, Patient, Donor);

It is interesting to note you can do this with a template that guarantees the fixed-array size via deduction.

template<size_t N>
void compare(int &i, int &i1, Person (&Patient)[N], Person (&Donor)[N])
{
    // N is guarenteed to be the size of your array. You can use it
    //  as you would 50 in your code.
    for (size_t i=0; i<N;++i)
    {
        // do something with Patient and Donor elements
    }
}

This has the added benefit of allowing instantiating with different array sizes. I.e. You can also do this:

Person Patient[50], Donor[50];
Person MorePatients[10], MoreDonors[10];

....

compare(i, i1, Patient, Donor);
compare(i, i1, MorePatients, MoreDonors)

and it will compile correctly. I suggest you experiment with it, you may find it useful.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
1

In addition to what WhozCraig has said, you could even put variadic template parameters in there to allow passing any number of types of things you can find in a hospital, such as nurses and midwives!

template <typename T1, size_t N1>
void compare(int &i, int &i1, T1 (&array)[N1]) {
    for (const auto& elem : array) {
        std::cout << elem.name << std::endl;   
    }
}

template <typename T1, size_t N1, typename... Ts, size_t... Ns>
void compare(int &i, int &i1, T1 (&array)[N1], Ts (&... arrays)[Ns]) {
    for (const auto& elem : array) {
        std::cout << elem.name << std::endl;   
    } 
    std::cout << std::endl;
    compare(i, i1, arrays...);
}

struct Nurse
{ std::string name; };

struct Midwife
{ std::string name; };


int main() {
    Nurse nurses[] = {{"Nina"}, {"Austin"}};
    Midwife midwives[] = {{"Matriona"}, {"Lizbeth"}, {"Jill"}};
    Nurse evenMoreNurses[] = {{"Maria"}, {"Nick"}, {"Martine"}, {"Ashley"}};

    int i{};
    int i1{};

    compare(i, i1, nurses, midwives, evenMoreNurses);
}

Do you realize now just how fun C++ can be?

0

just declare function like this:

void compare(int i, int i1, Person *Patient, Person *Donor);

you can invoke like:

compare(i, i1, Patient, Donor)

micheal.yxd
  • 118
  • 1
  • 7