-3
typedef struct arg_struct {
struct GPU_FFT *fft;
struct GPU_FFT_COMPLEX *base;
float **input;
float *output;
int number;
} arg_struct;

...

arguments[0].input = **Queue;
arguments[1].input = *(*(Queue)+QueueSize[0]);

My multidimensional array is Queue[2][1025]. I am trying to pass Queue[0][0] and Queue[1][0] into my arguments structure. It gives me "error: incompatible types when assigning to type ‘float **’ from type ‘float’" error. As a rookie programmer, I've tried so many variations but still couldn't figure out how to pass them.

By the way, QueueSize[0] is an integer which has value of 1025.

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
Pyro
  • 17
  • 10
  • 4
    [An array of arrays is *not* the same as a pointer to a pointer](https://stackoverflow.com/questions/18440205/casting-void-to-2d-array-of-int-c/18440456#18440456). – Some programmer dude Aug 14 '17 at 10:50
  • I always thought arrays and pointers are related to each other. – Pyro Aug 14 '17 at 10:51
  • Yes, C language has both arrays and pointers. A *lvalue* of array type decays into a pointer... and then, that's it. – Antti Haapala -- Слава Україні Aug 14 '17 at 10:53
  • *An* array can decay to a pointer to its first element. In your case `Queue` can decay to a pointer to an array of `1025` elements of type `float` (I guess), i.e. `Queue` is the same as `&Queue[0]` which is of the type `float (*)[1025]`. That in turn does *not* decay, because it's no longer an array. – Some programmer dude Aug 14 '17 at 10:54
  • See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) to unlearn some bad things. – Lundin Aug 14 '17 at 10:56

2 Answers2

2

You can solve your problem very easily, by making the input member a simple pointer:

float *input;

Then you can make each pointer point to the corresponding array of Queue:

arguments[0].input = Queue[0];
arguments[1].input = Queue[1];

Be careful though, the lifetime of Queue must be at least as long as the lifetime of arguments. If Queue goes out of scope then the pointers will be stray, and can no longer be used. If Queue can go out of scope then you need to create full arrays of (or allocate memory for) the input member, and copy the contents into that memory.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

There is big difference between 2d array and pointer to pointer.

In your struct is pointer to pointer, so you have to init it like

arguments[0].input = malloc(sizeof(float *) * num_of_rows);
for (int i = 0; i < num_of_rows; i++)
{
    arguments[0].input[i] = malloc(sizeof(float) * num_of_cols);
}

Dont forget to free all the allocated memory. You can use valgrind for checking memory leaks.


2D array is sequence of bytes in memory

  0   1   2   3
+---+---+---+---+
| 0 | 0 | 0 | 0 |   0  // begins on address 1000
+---+---+---+---+
| 0 | 0 | 0 | 0 |   1  // begins on address 1004
+---+---+---+---+
| 0 | 0 | 0 | 0 |   2  // begins on address 1008
+---+---+---+---+
| 0 | 0 | 0 | 0 |   3  // begins on address 1012
+---+---+---+---+ 

while array of pointers, where every pointer can point to another part of memory

  0   1   2   3
+---+---+---+---+
| * | * | * | * |
+---+---+---+---+
  |   |   |   |
  |   |   |   |    0   1   2   3
  |   |   |   |  +---+---+---+---+
  |   |   |   -> | 0 | 0 | 0 | 0 |   // begins on address 1000
  |   |   |      +---+---+---+---+
  |   |   -----> | 0 | 0 | 0 | 0 |   // begins on address 2
  |   |          +---+---+---+---+
  |   ---------> | 0 | 0 | 0 | 0 |   // begins on address 3045
  |              +---+---+---+---+
  -------------> | 0 | 0 | 0 | 0 |   // begins on address 128
                 +---+---+---+---+ 
kocica
  • 6,412
  • 2
  • 14
  • 35
  • 1
    "*In your struct is array of pointers*" well no, there is a pointer to a pointer (`float ** input`), nothing more, no array there. – alk Aug 14 '17 at 10:54
  • 1
    Dear Filip, please take this as kind advise: Although English might not be your 1st language, still, this subject requires very accurate wording. A "*pointer to a pointer*" stays what it is: a pointer to a pointer. It *never* "*could by [an] array of pointers*". Take and keep this mantra: "*Arrays are not pointers and pointers are not arrays!*" :-) – alk Aug 14 '17 at 10:58
  • I thought that in context of 2D arrays it would be helpful to mention that it can be used as array of pointers. – kocica Aug 14 '17 at 11:00
  • If you *use* your head as a hammer doing so does not *make* it a hammer, right? ;-) – alk Aug 14 '17 at 11:02
  • Sure not, Is it better now ? :-) – kocica Aug 14 '17 at 11:09