0

How can I convert from a double ** to a const double**

I am using ROOT and with a deconvolution function. When running, I receive an error:

cannot initialize a parameter of type 'const Double_t **' (aka 'const double **') with an lvalue of type 'Double_t **'(aka 'double **')

I define the parameter as such:

Double_t ** resp = new Double_t * [ybins]; for (int f = 0; f < xbins ; f = f+1){ for (int i = 0; i < ybins; i = i+1){ resp[f][i] = hinr->GetBinContent(i+1,f+1); } } 
2
  • 1
    Your code is broken anyway: you create a dynamic array resp of ybins double* which are not initialised (resp[f] can be anything, but certainly does it not point to an array of doubles); then you assign to resp[f][i]. Commented Jul 24, 2015 at 17:17
  • 1
    Show us the real code. There is no const in the code snippet you have given us, so how did you get that error? Commented Jul 24, 2015 at 17:23

1 Answer 1

0

Note: the answer to the "duplicate" question explains why the implicit cast does not work but it doesn't answer this question, "How do I do what I need to do?" That answer is:

Use either a c style cast or a c++ style const_cast. The c++ const_cast is preferred because it makes your intent more clear.

#include <iostream> int main(int argc, char * argv[]) { double a = 3.14159; double * b = & a; double **c = & b; const double ** d = (const double **) c; std::cout << **d << std::endl; // const_cast is the preferred approach. const double ** e = const_cast<const double **> (c); std::cout << **e << std::endl; return 0; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.