0
#include <iostream>
int test( const double *t1,const double **t2 )
{
  return 0;
}
int main(int argc, char*argv[])
{
  double *t1 = new double;
  const double ** t2 = new double *;
  test(t1, t2);
}

The error is :

cannot convert double ** to const double **

It compiles if I remove the 2 occurence of const though..

Learning
  • 219
  • 1
  • 6
  • 2
    @Diff. He clearly states what the error is- unless he edited it and it's not showing up as edited. – Puppy Jun 29 '11 at 09:25
  • possible duplicate of [Why does the following give me a conversion error from double *** to const double***](http://stackoverflow.com/questions/1404738/why-does-the-following-give-me-a-conversion-error-from-double-to-const-double) – Suma Jun 29 '11 at 10:47

4 Answers4

3

Make it

const double ** t2 = new const double *;

Diff.Thinkr
  • 855
  • 1
  • 8
  • 16
  • @Diff.Thinkr ,Isn't `new const double *;` and `new double *;` the same ? Why `new` needs to know whether it's `const` since it just allocates space from heap? – Learning Jun 29 '11 at 09:30
  • This might answer your doubt... http://stackoverflow.com/questions/1927477/can-a-heap-allocated-object-be-const-in-c – Diff.Thinkr Jun 29 '11 at 09:37
  • This gives a better explanation http://stackoverflow.com/questions/1370042/why-is-const-correctness-specific-to-c/1370121#1370121 – Diff.Thinkr Jun 29 '11 at 09:42
  • Quoting from the above link ,"If an object or data item is const, the compiler is free to place it in read-only memory. This can particularly matter in embedded systems. C++ supports this; few other languages do. This also means that, in the general case, you cannot safely cast const away, although in practice you can do so in most environments." So, I'm guessing it needs const to place it in the read-only memory. – Diff.Thinkr Jun 29 '11 at 09:47
  • @Diff.Thinkr,how to do the allocation here in `c`? – Learning Jun 29 '11 at 09:53
  • `const double **t2=(const double **)malloc( sizeof(double*) );` should work. The explicit cast is required only if you might want to use the same code in C++ although this is not recommended. Dont forget to `free(t2);` when use is over. – Diff.Thinkr Jun 29 '11 at 10:00
  • @Diff.Thinkr,so in C it doesn't need to know whether it's `const` when allocating memory,right? – Learning Jun 29 '11 at 10:03
  • malloc returns a `void*`. It seems C doesn't need to know about the constness unlike C++.But, I would recommend C++ & new where ever possible. – Diff.Thinkr Jun 29 '11 at 10:07
2

The issue is that by de-referencing in a certain way, you can violate const correctness with double pointers.

Puppy
  • 144,682
  • 38
  • 256
  • 465
0

it should at least compile now

int main(int argc, char*argv[])
{
  double *t1 = new double;
  const double ** t2 = new const double *;
  test(t1, t2);
}
arrowd
  • 33,231
  • 8
  • 79
  • 110
Robert Parcus
  • 1,029
  • 13
  • 19
0

Such conversion is not allowed, because if the conversion would be possible, you could modify the const object in a following way:

#include <stdio.h>

const double A0 = 0;
const double A1 = 1;
const double* A[2] = { &A0, &A1 };
double * B[2];

int main()
{
  double** b = B;
  const double ** a = b; // illegal
  //const double ** a = (const double **)b; // you can simulate it would be legal

  a[0] = A[0];
  b[0][0] = 2; // modified A0

  printf("%f",A[0][0]);
}

For a simulated result, check the code at IdeOne.com - you will get SIGSEGV (const object was placed in read only memory and you are trying to modify it). With a different platform the object might be modified silently.

Suma
  • 33,181
  • 16
  • 123
  • 191