2

My compiler gives me this warning: passing argument 2 of 'transform_labels' from incompatible pointer type [-Wincompatible-pointer-types] with this note: expected 'int (*)[10]' but argument is of type 'int **'

My code:

void transform_labels(int array[60000], int labels[60000][10], int NPAT){

    for(int i = 0; i < NPAT; i++){

        int aux = array[i];
        labels[i][aux] = 1;
        printf("%d\n ",*labels[i]);
        if ((i+1) % 10 == 0) putchar('>');

    }

}

int main() {

   load_mnist();
   int loop;
   int** labels;
   allocate_mem(&labels, 60000, 10);
   printf("HERE");
   transform_labels(train_label, labels, 60000);
   return 0;

}
  • 2
    This have been asked many times before. The problem is `int**`, which has nothing to do with arrays. [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) might be helpful. – Lundin Dec 12 '19 at 14:38

2 Answers2

5

A pointer to a pointer cannot be converted to a pointer to an array. While an array can be converted to a pointer that only applies to the outermost dimension of a multidimensional array.

You need to change the declaration of your function:

void transform_labels(int *array, int **labels, int NPAT){
dbush
  • 205,898
  • 23
  • 218
  • 273
0

You are allowed to pass a pointer instead of the first dimension of a function argument, and vice-versa, but all other dimensions must match. You have a second dimension [10].

You can pass it a pointer to an array of size 10, but that might just push your problem up to another point in the code, such as your allocate function. The following should compile, but it is not clear that this is what you want:

typedef int LabelSub[10];
LabelSub* labels;
allocate_mem(&labels, 60000, 10);
Gem Taylor
  • 5,381
  • 1
  • 9
  • 27
  • Hiding arrays behind typedefs is often just as bad as hiding pointers behind typedefs. Instead just do `int labels[60000][10]`. Also, all you did with this code was to move the incompatible pointer type error to `allocate_mem`. – Lundin Dec 12 '19 at 14:37
  • @Lundin Indeed. The ain of the typedef was not to "hide" it but just to make it writable. I find the syntax for mixing pointers and array almost unreadable.And yes, it will depend on what type the allocate function actually returns. This is the "other" poiunt in the code. – Gem Taylor Dec 12 '19 at 15:37