Error in pointer function

The below function is showing an error :
C:\Users\my pc\Documents\C++\Slide7Pointer.cpp [Error] invalid conversion from 'int' to 'int*' [-fpermissive]
Please tell where is the mistake and how to correct it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;

int main()
{
int m=44, n=22;
int max(int &, int &);
int  *q=max(m,n);
cout<<"m="<<m<<"\t"<<"n="<<n<<"  *q"<<*q<<endl;
m=55;
cout<<"m="<<m<<"\t"<<"n="<<n<<"  max(m,n)="<<max(m,n)<<endl;
}

int  &max(int &m,int &n)
{
return(m>n?m:n);
}
Last edited on
The function max() is returning a reference on line 16

You are assigning a reference to a pointer on line 8, which is illegal

So how should I correct it. ?
int & is a reference to an int, not a pointer to an int. On line 8, you are trying to assign the result of max() to a pointer. Instead, make it a reference, like what your actual function returns.

On line 7, your prototype does not match your function definition on line 14. Your prototype should declare the return type as an int&, not an int.

Also, don't be afraid of whitespace... IMO it really helps readability.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>

using namespace std;

int main()
{
    int& max(int &, int &); // prototype
    
    int m = 44;
    int n = 22;

    int& q = max(m,n);

    cout << "m = " << m << "\t" << "n = " << n << "  q = " << q << endl;
    m = 55; // q is a reference to m now
    cout << "m = " << m << "\t" << "n = " << n <<"  max(m,n) = " << q << endl;
      // q is updated because it's a refeernce to m
}

int& max(int &m, int &n)
{
    return (m > n ? m : n);
}
Thanks. But suppose I want to keep line 9-11 in my question code same, is there any change which I can do in function
Last edited on
Make the max function return an int* ("pointer to int") then, and change your prototype (line 7) accordingly to match.

Edit: I don't see a way for you to keep lines 9-11 completely unchanged. You'd have to also cout << *max(m,n) as well then.
Last edited on
If you don't want 'q' to be a reference, then make max() return a pointer

1
2
3
4
5
int* max(int &m, int &n)
{

    return (m > n ? &m : &n);
}
What should I return? I did upto this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>

using namespace std;

int main()
{
    int* max(int &, int &); // prototype
    
    int m = 44;
    int n = 22;

    int  *q =  max(m,n);
cout<<"m="<<m<<"\t"<<"n="<<n<<"  *q"<<*q<<endl;
m=55;
cout<<"m="<<m<<"\t"<<"n="<<n<<"  max(m,n)="<<max(m,n)<<endl;

}

int* max(int &m, int &n)
{
    return (m > n ? m : n);
}
Last edited on
whoops, right I forgot that one detail

Gotta change your max function to
1
2
3
4
int* max(int &m, int &n)
{
    return (m > n ? &m : &n);
}

So that it returns the address of the variable.

See also: http://www.cplusplus.com/doc/tutorial/pointers/ for more pointer examples.
Last edited on
Got it. Thanksa lot.
Registered users can post here. Sign in or register to post.