4

Consider the following declarations :

const float** b;
const float* const* c;
float* const* d;

First off all I want to make sure that :

  • b is a pointer to pointer to const float.

  • c is a pointer to const pointer to const float.

  • d is a pointer to a const pointer to float.

Now my question is how can I initialize each pointer using new operator?

adem sonmez
  • 153
  • 7

1 Answers1

4
const float** b = new const float*();
const float* const* c = new const float*(nullptr);
float* const* d = new float*(nullptr);

the type with one less indirection and set values if they point on const

Tyker
  • 2,971
  • 9
  • 21
  • 3
    wow. this is putting a loaded handgun into the hands of a child! – Richard Hodges May 15 '18 at 12:19
  • 1
    @RichardHodges he asked for it – Tyker May 15 '18 at 12:20
  • Thanks for answer, but I noticed that the compiler gives no error when I omit `const` keyword on the right hand side in some cases. For instance: `const float* const* b = new const float*` `const float* const* b = new float*` `const float* const* histRange=new float*((float*)(new float(45)))` `const float* const* histRange=new const float*((float*)(new float(45)))` all of them work. (At least they are compiled without an error). But this gives error: `const float** b = new float*` I really don't understand when I have to use `const` and when I don't have to. – adem sonmez May 15 '18 at 15:00
  • @ademsonmez `const float**` is a pointer on pointer on constant float so you need to new a pointer on constant float – Tyker May 15 '18 at 15:14
  • @Tyker How about `const float* const* histRange=new float*(nullptr)` it is also `pointer to const pointer to const float` but you can initialize it without using `const` – adem sonmez May 15 '18 at 16:32
  • @ademsonmez this comes from implict convertions, in `const float* const* histRange=new float*(nullptr)` you are converting a `float**` to `const float * const *` wich is allowed but converting from from `float**` to `const float**` isn't, see https://stackoverflow.com/questions/2220916/why-isnt-it-legal-to-convert-pointer-to-pointer-to-non-const-to-a-pointer-to for more detail – Tyker May 15 '18 at 17:31