Hi everybody. I wrote down two functions, but after compiling they return rubbish. As I understand they should return 5 and 7. Here is a code. Can somebody help me?
return i<j;
this will return a bool and not the largest/smallest value cout<<*c<<endl;
c is an uninitialized pointer, I assume you meant to call func before.
i=&j;
this will give pointer i the adress of j, but won't change the value of the parameter passed into the function, try: *i=j;
# include <iostream>
usingnamespace std;
int compare(int i, int j)
{
return (i<j)?j:i;
}
// No need for this function
int main()
{
int a=4, b=7;
int *c, f=5;
c = &f;// that's problem of your problem c point at location in memory
cout << *c << endl;
cout << compare(a,b) << endl;// I assume that you want the greatest value between 5 and 7
return 0;
}
I tried to avoid using that short hand because it makes code more complex, especially for beginners, essentially what return (i<j)?j:i;
does is evaluate a statement and return the first value if it's true and the second if it's false.
example: