D. Maul D. Maul - 4 months ago 9
C Question

function that returns a pointer; In C

I imagine this to be a relatively simple answer. I am just getting used to pointers and I am just about done however I cannot figure out this error:


PtrBasicBCGold.cc:27:13: error: no matching function for call to
'HelperFunc' maxPtr = HelperFunc(&i1, &i2, &i3, &i4);

^~~~~~~~~~ PtrBasicBCGold.cc:3:6: note: candidate function not viable: no known conversion from 'int **' to 'int *' for 1st
argument; remove & int *HelperFunc(int *i1, int *i2, int *i3, int
*i4)

^ 1 error generated.


The prompt is to keep the below setup of an increment function and a helper. But I cannot figure out how to return the pointer without an error of this sort.

#include <stdio.h>

int *HelperFunc(int *i1, int *i2, int *i3, int *i4)
{

if(*i1 > *i2 && *i1 > *i3 && *i1 > *i4)
{
return i1;
}
if(*i2 > *i1 && *i2 > *i3 && *i2 > *i4)
{
return i2;
}
if(*i3 > *i1 && *i3 > *i2 && *i3 > *i4)
{
return i3;
}
else return i4;
}

void IncrementMax(int *i1, int *i2, int *i3, int *i4)
{
int *maxPtr;
maxPtr = HelperFunc(&i1, &i2, &i3, &i4);
*maxPtr = *maxPtr + 1;
}


int main() {
int i1, i2, i3, i4;

scanf("%d %d %d %d", &i1, &i2, &i3, &i4);

IncrementMax(&i1, &i2, &i3, &i4);

printf("%d %d %d %d", i1, i2, i3, i4);

return 0;
}

Answer

The problem is in this line:

maxPtr = HelperFunc(&i1, &i2, &i3, &i4);

You function call does not match with your prototype. Note that i1, i2, etc. are already pointers, so you do not need & which is the address of. If you do so it would be pointer to pointer to int, which is not what you need.

This change should fix the issue:

maxPtr = HelperFunc(i1, i2, i3, i4);