如何传递和 return 指向结构数组的指针?
How to pass and return a pointer to an array of structures?
我有一个结构数组,结构如下所示:
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];
我有两个功能如下:
/********* function declaration *********/
Patient *search_patient(Patient patientregister[], int num_of_patients);
Patient *search_by_personaNumber(Patient *matches[],
Patient patientregister[], int num_of_patients);
代码从*search_patient
开始,然后到*search_by_personalNumber
。 *search_patient
在其中声明了另一个结构数组:Patient matches[5];
,其想法是将 Patient matches[5];
的指针发送到 *search_by_personalNumber
。然后将其与用户正在搜索的匹配项一起返回到 *search_patient
。我的问题是如何将结构数组的指针发送到另一个函数,使用指针填充结构数组并将指针发送回原始函数,在我的例子中是 *search_patient
?
数组被隐式地(除了极少数例外)转换为指向它们在表达式中第一个元素的指针。
因此,如果在函数 search_patient
中声明了这样一个数组
Patient *search_patient(Patient patientregister[], int num_of_patients)
{
Patient matches[5];
//...
}
然后将它传递给函数search_by_personaNumber
你可以通过以下方式
Patient *search_patient(Patient patientregister[], int num_of_patients)
{
Patient matches[5];
//...
search_by_personaNumber( matches, 5 );
//...
}
其实在函数search_patient
中没有必要使用函数search_by_personaNumber
的return值。但是如果你确实需要使用它那么你可以写
Patient *search_patient(Patient patientregister[], int num_of_patients)
{
Patient matches[5];
//...
Patient *p = search_by_personaNumber( matches, 5 );
//...
}
我有一个结构数组,结构如下所示:
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];
我有两个功能如下:
/********* function declaration *********/
Patient *search_patient(Patient patientregister[], int num_of_patients);
Patient *search_by_personaNumber(Patient *matches[],
Patient patientregister[], int num_of_patients);
代码从*search_patient
开始,然后到*search_by_personalNumber
。 *search_patient
在其中声明了另一个结构数组:Patient matches[5];
,其想法是将 Patient matches[5];
的指针发送到 *search_by_personalNumber
。然后将其与用户正在搜索的匹配项一起返回到 *search_patient
。我的问题是如何将结构数组的指针发送到另一个函数,使用指针填充结构数组并将指针发送回原始函数,在我的例子中是 *search_patient
?
数组被隐式地(除了极少数例外)转换为指向它们在表达式中第一个元素的指针。
因此,如果在函数 search_patient
中声明了这样一个数组
Patient *search_patient(Patient patientregister[], int num_of_patients)
{
Patient matches[5];
//...
}
然后将它传递给函数search_by_personaNumber
你可以通过以下方式
Patient *search_patient(Patient patientregister[], int num_of_patients)
{
Patient matches[5];
//...
search_by_personaNumber( matches, 5 );
//...
}
其实在函数search_patient
中没有必要使用函数search_by_personaNumber
的return值。但是如果你确实需要使用它那么你可以写
Patient *search_patient(Patient patientregister[], int num_of_patients)
{
Patient matches[5];
//...
Patient *p = search_by_personaNumber( matches, 5 );
//...
}