Sai Shankar Sai Shankar - 4 months ago 15
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)
else if (Arr[i] > score)
return lessCount; // <-- I need to return lesscount and morecount..!

How can I return both values using void pointer?

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