0

I have an array of structures and the structure looks as below:

struct patient {
    int pictures[2];
    int personal_number;
    char patient_name[FILE_PATIENT_NAMES + 1];
    int num_of_matches;
};

typedef struct patient Patient;

Patient patientregister[5];

I have two functions as down below:

/********* function declaration *********/

Patient *search_patient(Patient patientregister[], int num_of_patients);
Patient *search_by_personaNumber(Patient *matches[],
                                 Patient patientregister[], int num_of_patients);

The code starts from *search_patient and then goes to *search_by_personalNumber. *search_patient has another array of structures declared inside it: Patient matches[5]; and the idea is to send a pointer of Patient matches[5]; to *search_by_personalNumber. and then get it back to *search_patient with the matches the user is searching for. My question is how to send a pointer of an array of structures to another function, use the pointer to fill in the array of structures and send the pointer back to the original function, in my case *search_patient?

1
  • The formal parameter Patient *matches[] will never be compliant an argument from Patient matches[5] in any way whatsoever. Either the argument, or the formal parameter has to change somehow. Regardless, as we have no idea what this code is even supposed to be doing, we can't honestly assist further than pointing out that non-trivial fact. I suspect you're confusing a "pointer to an array of structures" with an "array of pointers to structures", but the fog is pretty thick to make that wild assumption. Commented Jun 25, 2019 at 17:58

1 Answer 1

1

Arrays are implicitly (with rare exceptions) converted to pointers to their first elements in expressions.

So if in the function search_patient you declared an array like this

Patient *search_patient(Patient patientregister[], int num_of_patients)
{
    Patient matches[5];
    //...
}

then to pass it to the function search_by_personaNumber you can the following way

Patient *search_patient(Patient patientregister[], int num_of_patients)
{
    Patient matches[5];
    //...

    search_by_personaNumber( matches, 5 );
    //...
}

In fact there is no need in the function search_patient to use the return value of the function search_by_personaNumber. But if you indeed need to use it then you can write

Patient *search_patient(Patient patientregister[], int num_of_patients)
{
    Patient matches[5];
    //...

    Patient *p = search_by_personaNumber( matches, 5 );
    //...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.