Sai Shankar Sai Shankar - 5 months ago 24
C Question

Return value void * in a function

I was given a function whose return value is

void *
. I need to return two values from that function.

void * studentsCount(int *Arr, int len, int score, int *lessCount, int *moreCount) {
if (Arr == NULL || len <= 0)
return NULL;
for (int i = 0; i < len; i++){
if (Arr[i] < score)
*lessCount++;
else if (Arr[i] > score)
*moreCount++;
}
return lessCount; // <-- I need to return lesscount and morecount..!
}


How can I return both values using void pointer?

P0W P0W
Answer Source

You don't have to return moreCount and lessCount, you just have to update their values inside the function.

As per return type void * is concerned, I would assume it signifies if the passed array is a "valid" array, if it is you may choose to return a non null value ( depends on how the studentsCount function is invoked ).

The caller should also make sure moreCount and lessCount points to a valid memory location, initialized to proper values