Printf: cannot convert ‘double*’ to ‘double (*)

Hey

I am trying to take the inverse Fourier transform of complex output and print out in a 2D array. I wrote a printf function (that seems part of the error) and tried to take inverse FFT using fttw.3 library which is working. But for some reason, I cannot print it.

The printf function is called: print2DPhysArray in arithmetic functions.

Error message:
1) From the printf function I wrote
1
2
3
4
5
warning: format ‘%le’ expects argument of type ‘double’, but argument 2 has type ‘double*’ [-Wformat=]
 printf("%+4.2le  ",arr[j + ny*i]); //[j + ny*i] real. from arr[i][j]). from: printf("%+4.2le  ",arr[j + ny*i]);
      |            ~~~~~~^                ~
      |                  |                |
      |                  double           double*


2) From the cpp file where I am trying to print out results

1
2
3
4
5
6
7
8
error: cannot convert ‘double*’ to ‘double (*)[512]’
  885 |  print2DPhysArray(potSource_inertia); //try *
      |                   ^~~~~~~~~~~~~~~~~
      |                   |
      |                   double*
In file included from spectralFunctions.cpp:7:
arithmeticFunctions.h:9:30: note:   initializing argument 1 of ‘void print2DPhysArray(double (*)[512])’
    9 | void print2DPhysArray(double arr[][ny]);


I will be including my code since it would be easier to see the problem:
https://www.zipshare.com/download/eyJhcmNoaXZlSWQiOiJlMzc5ODU3ZS02ZjNjLTQ5ZGQtYmVlYS02OTE0N2M0NmY2YTgiLCJlbWFpbCI6ImFzdHJvbHVqeUBnbWFpbC5jb20ifQ==
It looks like arr is a two dimensional array, but you are only providing one dimension.
I thought I was providing 2D. Then how could I pass 2D or provide 2D instead?
I understand this is a pointer to an array, and I am trying to print the array as a 2D matrix
You are printing your array element as if it's a 1d array (with 2d mapping logic).
You are passing your array as if it is a 2d array, but it would appear that potSource_inertia is a 1d array.

I would try making everything consistent:
- Change function to void print2DPhysArray(double arr[]);
- Pass a 1d array to it as you appear to already be doing.
- Keep your printf statement the same.

Btw, if your width is not the same value as your height, make sure your stride is the right length. If the width of the array is "width", then you would access each index as arr[width * i + j]. The height doesn't matter for the indexing.
Last edited on
Thanks! This works, I am still learning to understand these error messages.

I have already written a printf 1D array function that I could've used if I understood the error.
Topic archived. No new replies allowed.