Search code examples
cpointersmultidimensional-arraywarningsc99

Warning: assignment to 'double (*)[101]' from incompatible pointer type 'double *'


This warning has been hurting me for a few weeks. I made a C program with main.c, basic_math.c, basic_math.h, and variables.h. This program transfers a two-dimensional matrix from main.c to function.c, calculates the square of each element of the matrix in the function.c, and returns the result into the main.c. I can get good result from this program. Nevertheless I want to know why I got this warning and receive a good piece of advice about how to use two-dimensional matrix with the function in external program like basic_math.c.

main.c

#include <stdio.h>
#include <math.h>

#include "basic_math.h"

int main()
{
  #include "variables.h"
  #include "open_read.h"
      
  //square
  sq_matrix = square(signal, sizeof(signal)/sizeof(signal[0]));
  
  //test
  for(int j=0; j <y; j++){
    for(int i=0; i <x; i++){
      printf("%lf \n", sq_matrix[j][i]);
    }
  }
#include "write.h"
#include "close.h"

  return 0;
}

basic_math.h

double * square(double signal[][101], int y);

basic_math.c


double * square (const double signal[][101], int y)
{
   static int x3 = sizeof(signal[0]/sizeof(double));
   int y3 = y;
   static double temp_sq[4096][101];

   for(int j=0; j <y3; j++){
   for(int i=0; i <x3; i++){
                           temp_sq[j][i] = signal[j][i] * signal[j][i];
                          }
                          }
   return temp_sq;
}

variables.h

int y=4096;
int x=101;

double signal[y][x];
double (*sq_matrix)[x];

compile

gcc -g main.c /path/basic_math.c -o test

warning after compiling

main.c: In function ‘main’:
main.c:22:14: warning: assignment to ‘double (*)[101]’ from incompatible pointer type ‘double *’ [-Wincompatible-pointer-types]
   22 |    sq_matrix = square(signal, sizeof(signal)/sizeof(signal[0]));
      |              ^
/path/basic_math.c: In function ‘square’:
/path/basic_math.c:47:12: warning: returning ‘double (*)[101]’ from a function with incompatible return type ‘double *’ [-Wincompatible-pointer-types]
   47 |     return temp_sq;

I omitted the parts to open, write, and close the file for convenience. I am a beginner of the pointer, two-dimensional matrix, and external c program. And, referring to this, I used 'static' in basic_math.c in order to to transport the matrix from external to main program.

I fixed the size of the signal[y][x] in variables.c and enterred the size of the temp_sq once more in the basic_math.c. This is inconvenient for me. In fact, when I use this program, the number of column and row could be variable. If I can just control the size of the matrix in the main.c, this program would be much more effective.


Solution

  • main.c: In function ‘main’: main.c:22:14: warning: assignment to ‘double (*)[101]’ from incompatible pointer type ‘double *’ [-Wincompatible-pointer-types] 22 | sq_matrix = square(signal, sizeof(signal)/sizeof(signal[0])); | ^

    This is telling you that the function is returning a pointer to double b (double *), which is being assigned to a pointer to a multidimensional array with row length 101 (double (*)[101]).

    /path/basic_math.c: In function ‘square’: /path/basic_math.c:47:12: warning: returning ‘double (*)[101]’ from a function with incompatible return type ‘double *’ [-Wincompatible-pointer-types] 47 |
    return temp_sq;

    This is telling you that the function is returning a pointer to double b (double *), using the value of temp_sq, which is a pointer to a multidimensional array with row length 101 (double (*)[101]).

    This can be corrected by changing the return type to match the source and destination for the return value. For example:

    typedef double (*Double2DArr101)[101];
    
    Double2DArr101 square (const double signal[][101], int y)