<分区>
我有以下结构和主要功能:
struct myStruct{
string name;
int id;
int group;
};
int main(){
myStruct student[5]; // The struct student has an array of 5 elements
search(student, 1, 33); // We pass the struct student with all the 5 elements
}
我想将一个结构传递给函数搜索,然后创建一个数组指针来存储某个属性的值,但该结构的所有数组。
*e 指向学生,所有数组(5),所以如果类型等于 1,指针将指向结构 e 的每个数组的属性的所有值
void search(myStruct *e, int type, int value){
if (type == 1) int *ptr[] = e[0]->id; //An int pointer because the id is an int
if (type == 2) int *ptr[] = e[0]->group;
for (int i = 0; i < 5; i++){
if(*ptr[i] == value){
cout << e[i]->name << endl;
cout << e[i]->id << endl;
cout << e[i]->group << endl;
}
}
}
我希望 *ptr[]
指向属性的每个数组,具体取决于传入的参数类型。例如:
if ( type == 1 )
ptr[0] = e[0].id;
ptr[1] = e[1].id;
ptr[2] = e[2].id;
ptr[3] = e[3].id;
ptr[4] = e[4].id;
^Notice that is just the id
if ( type == 2 )
ptr[0] = e[0].group;
ptr[1] = e[1].group;
ptr[2] = e[2].group;
ptr[3] = e[3].group;
ptr[4] = e[4].group;
^Notice that is just the group
问题是我找不到办法做到这一点,我程序中的真正结构不仅仅是三个属性,它实际上有八个,所以如果我为每一个。 感谢您的帮助。