0

GCC gives me an 'Initialization from incompatible pointer type' warning when I use the following code.

 #include "mnist.h"
 #include <stdio.h>

 int main(void)
 {

    double** inputLayer = train_image; 

}

train_image is a bidimensional array given by mnist header file that according to the autor is like that

  • train image : train_image[60000][784] (type: double, normalized, flattened);
  • 1
    Two dimensional array *is not* a pointer to pointer. – Eugene Sh. Dec 09 '19 at 17:26
  • 1
    While array decays to pointers to their first element, in this case the array `train_image` decays to a pointer to an *array* (which does *not* decay further). A pointer to an array is not the same as a pointer to a pointer. [This old answer of mine](https://stackoverflow.com/questions/18440205/casting-void-to-2d-array-of-int-c/18440456#18440456) tries to show the difference. – Some programmer dude Dec 09 '19 at 17:27
  • How can I change the code to work? – Francisco Resende Dec 09 '19 at 17:42

2 Answers2

0

array is not pointer to pointer.

int main()
{
    double *pointer = &train_image[0][0];
    double **inputLayer = &pointer; 
}
0___________
  • 60,014
  • 4
  • 34
  • 74
0

If you have an array like this

double train_image[60000][784];

then a pointer to its first element should be declared like

double ( *inputLayer )[784] = train_image;

That is the expression train_image used as an initializer has the type double ( * )[784].

If it is needed then you can reinterpret the two-dimensional array as a one dimensional array using the following pointer declaration

double *inputLayer = ( double * )train_image;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335