blitzeus
blitzeus

Reputation: 495

returning pointers to doubles from pointers of const doubles

Im trying to write a function named ComputeMaximum that has two parameters, both of type pointer to constant double, and returns type pointer to double

I keep getting the following casting errors:

  1. invalid conversion from ‘const double*’ to ‘double*’
  2. cannot convert ‘double’ to ‘const double*’ for argument ‘1’ to ‘double* ComputeMaximum(const double*, const double*)’
#include <iostream>
using namespace std;

double *ComputeMaximum(const double *num1, const double *num2)
{
    return ((double*)num1>num2?num1:num2);  
}

int main()
{
    double *max;

    max = ComputeMaximum(6.4, 6.9);
    cout << *max;

    return 0;
}

Upvotes: 0

Views: 1753

Answers (5)

camelccc
camelccc

Reputation: 2992

if he actually wants to pass pointers and return a pointer (can't think why unless this is homework) If you want const double you will need to put const before every use of the word double in this entire clip

#include <iostream>
using namespace std;

double *ComputeMaximum(double *num1,double *num2)
{
    return (* num1 > * num2 ?num1:num2);  // need to dereference the comparison
}

int main()
{
    double *max;
    double a = 6.4;
    double b = 6.9; // need to be variables with real adresses, literals won't work

    max = ComputeMaximum(&a, &b); // pass pointers to a and b
    cout << *max;

    return 0;
}

Upvotes: 1

Antimony
Antimony

Reputation: 39451

You can also use const_cast to cast const values to non const values, but this is almost always a bad idea. The only legitimate use is interfacing with legacy libraries that expect strings as non const char pointers but don't actually modify them.

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61515

Alternative to Mahesh's nice solution you can store the doubles in variables before calling the ComputeMaximum function then use the addressof operator & to return their location in memory.

double a = 6.4; 
double b = 6.9; 

max = ComputeMaximum(&a, &b);

But then you would also have to change your function to compare the values pointed to instead of the pointers themselves:

double *ComputeMaximum(const double *num1, const double *num2)
{
    double a = *(num1);
    double b = *(num2);

    return (double*)(a > b ? a : b);  
}

Upvotes: 0

cybertextron
cybertextron

Reputation: 10961

you have an invalid param, and the compiler won't allow this kind of implicit conversion:

max = ComputeMaximum(6.4, 6.9);

Since the header file is:

double *ComputeMaximum(const double *num1, const double *num2)

You either pass a const double * as argument, or change your declaration to:

double *ComputeMaximum(const double num1, const double num2)
double *ComputeMaximum(const double &num1, const double &num2)

However, this is valid for your case:

double a = 6.4;
double b = 6.9;
max = ComputeMaximum(&a, &b);

You can either explicit cast the value, or change the declaration in your method, or use references, if you want efficiency:

Upvotes: 0

Mahesh
Mahesh

Reputation: 34615

6.4, 6.9 are floating point literals (or) constant values and cannot be converted to pointers. What you need is just double as the parameter type for the function but not double*.

Pointers points to the address of the location and cannot hold value itself. So, try

double ComputeMaximum(const double num1, const double num2)
// Notice * are removed.
{
   // ....
}

Upvotes: 2

Related Questions