0

I need to pass different array of structures to function, but I don't know how to do it. Each time, I'm getting an error. I go through the Internet but all examples I found are about passing specific or only one array of structure to function (Passing array of structures to function c++) but what if I have different structures each time and I want to pass it to the same function.

So I'm not sure if what I want to do is applicable or not.

To clarify more if I have the following structures:

struct EVRL_Mapping2 {
    double avgMap2;
    int permutationMap2[2];
};
struct EVRL_Mapping3 {
    double avgMap3;
    int permutationMap3[3];
};
struct EVRL_Mapping4 {
    double avgMap4;
    int permutationMap4[4];
};
EVRL_Mapping2 EVRL_Map2[2]; //Mapping2: AC3,AC2 (2 mapping)
EVRL_Mapping3 EVRL_Map3[2]; //Mapping3: AC3,AC2,AC1 (2 mapping)
EVRL_Mapping4 EVRL_Map4[1]; //Mapping4: AC3,AC2,AC1,AC0 (1 mapping)

Now in the .CC code, if I have an array of two elements of EVRL_Mapping2 (EVRL_Map2[2]) and I want to find the maximum of avgMap2 between the two elements of structure and return the index/location of that element. Is it going to be like this: if yes, does it mean for each structure I need to define a separate function!?

int findMax(struct EVRL_Mapping2 myarray[], int size){
for(int i=0;i<size;i++){
//  printf("myarray[%d]=%d\n",i,myarray[i]);
}
    double max = myarray[0].avgMap2;       
    int index = 0;
    for(int i = 1; i<size; i++)
    {
    if(myarray[i] > max){
        max = myarray[i].avgMap2;
        index = i;
        printf("Inside if(): index=%d\n",index);
    }
    }
    return index; 
printf("Inside findMax(): index=%d\n",index);
}

Now calling and passing to this function:

findMax(EVRL_Map2,2);

However, I have an error with the above function definition and the code doesn't compile.So can you advise?

Thanks for your help in advance.

3
  • It would be helpful to have the compilation error(s). Commented Apr 18, 2015 at 15:12
  • One option in C99 and later is to use a flexible array member: struct EVRL_MappingN { double aveMap; int perm_size; int permutationMap[]; };. This is probably what I'd use. Alternatively, use a pointer to the array: struct EVRL_MappingP { double aveMap; int perm_size; int *permutationMap; }; and use malloc() or equivalent to allocate the correct size of permutation map. Commented Apr 18, 2015 at 15:17
  • @mattm : The error apps/udp.cc:333: error: no matching function for call to ‘UdpAgent::findMax(UdpAgent::EVRL_Map2 [2], int)’ apps/udp.h:62: note: candidates are: virtual int UdpAgent::findMax(EVRL_Map2*, int) apps/udp.cc: At global scope: apps/udp.cc:599: error: prototype for ‘int UdpAgent::findMax(UdpAgent::EVRL_Map2*, int)’ does not match any in class ‘UdpAgent’ apps/udp.h:62: error: candidate is: virtual int UdpAgent::findMax(EVRL_Map2*, int) Commented Apr 18, 2015 at 15:34

0

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.