简体   繁体   中英

How to return pointer using conditional statement?

The following code outputs the second number as the maximum. If you need any other information please let me know.

#include <iostream>                                                     
#include <cstdlib>
using namespace std;
double *ComputeMaximum(const double *Max, const double *Min);

double *ComputeMaximum(const double *Max, const double *Min)
{
    return ((double *)((&Max > &Min) ? Max : Min));
}

int main(void)
{
    double *max;
    double Initial, Secondary;


    cout << "Enter the number followed by space then another number: ";
    cin >> Initial;
    cout << "\nIn-" << Initial;
    cin >> Secondary;
    cout << "\nSe-" << Secondary;
    //cout >> "Of " >> Inital >> "and " >> Secondary;
    //cout >> "the maximum is " >>
    max = ComputeMaximum((double*)&Initial,(double*)&Secondary);
    cout << "\nmax" << *max;
    return 0;
}

The next code outputs the first number as the maximum

#include <iostream>                                                     
#include <cstdlib>
using namespace std;
double *ComputeMaximum(const double *Max, const double *Min);

double *ComputeMaximum(const double *Max, const double *Min)
{
    return ((double *)((Max > Min) ? Max : Min));  // Here is the difference(& missing)
}

int main(void)
{
    double *max;
    double Initial, Secondary;


    cout << "Enter the number followed by space then another number: ";
    cin >> Initial;
    cout << "\nIn-" << Initial;
    cin >> Secondary;
    cout << "\nSe-" << Secondary;
    //cout >> "Of " >> Inital >> "and " >> Secondary;
    //cout >> "the maximum is " >>
    max = ComputeMaximum((double*)&Initial,(double*)&Secondary);
    cout << "\nmax" << *max;
    return 0;
}

What is being done wrong? I only need the maximum, not the second or first input. I got the answer. Thank YOu all.

Here it is:

#include <iostream>                                                     
#include <cstdlib>
using namespace std;
double *ComputeMaximum(const double *Max, const double *Min);

double *ComputeMaximum(const double *Max, const double *Min)
{
    return (double*)((*Max > *Min) ? Max : Min);
}

int main(void)
{
    double *max;
    double Initial, Secondary;


    cout << "Enter the number followed by space then another number: ";
    cin >> Initial;
    cout << "\nIn-" << Initial;
    cin >> Secondary;
    cout << "\nSe-" << Secondary;
    //cout >> "Of " >> Inital >> "and " >> Secondary;
    //cout >> "the maximum is " >>
    max = ComputeMaximum(&Initial, &Secondary);
    cout << "\nmax" << *max;
    return 0;
}

You're comparing addresses. The correct way would be:

double *ComputeMaximum(const double *Max, const double *Min)
{
    return *Max > *Min ? Max : Min;
}

Your versions:

(Max > Min)

compares the pointers themselves, and

(&Max > &Min)

compares the addresses of the pointers, which is, again, wrong.

Also, you don't need pointers, and note that you have std::max which you can use.

double *ComputeMaximum(const double *Max, const double *Min)
{
    return *Max > *Min ? Max : Min;
}

Note that I used *Max and *Min instead of &Max and &Min , ie dereferencing, not taking the address! Also note that you have a lot of unnecessary casts to double* for expressions that are already of the desired type. One exaple is your ComputeMaximum function body. Another example

max = ComputeMaximum((double*)&Initial,(double*)&Secondary);

Because Initial and Secondary are of type double , &Initial and &Secondary are of type double* so there is absolutely no need for the ugly unnecessary cast. Just use

max = ComputeMaximum(&Initial,&Secondary);

Similarly in other places.

I strongly recommend you to read a good book on C++ .

Why are you using pointer in the first place? It is not even needed.

The following is a better implementation:

double ComputeMaximum(double a, double b)
{
    return a > b ? a : b;
}

Or if you wish to do something like this:

ComputeMaximum(x,y) = 100; //modify the one which is maximum

then reference is what you need:

double & ComputeMaximum(double & a, double & b)
{
    return a > b ? a : b;
}

By the way, you may would like to see std::max (and std::min ) from the Standard library.

Neither of them returns the actual maximum.

You are passing pointers to doubles. So the value of the variables Min,Max are an address that points to a double.

&Max > &Min ... This will compare the addresses of the variables Max,Min which are local to the function.

Max > Min ... This will compare the addresses that Min and Max point to (just address numbers).

*Max > *Min ... This will dereference the pointers, and compare the object they are pointing at. This is what you want to do...

return ((double *)((*Max > *Min) ? Max : Min));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM