-1

I have a struct like:

struct oda {
    char isim[10];         
    int id[1];                       
};

and 2d table created with this type:

struct oda* tab[X][Y];

This table should be allocated dynamically on memory, so if we have product placement on x and y tab[X][Y] should point to our struct, otherway value of pointer tab[X][Y] = NULL

I have created a fonction for init this table:

void init_tab_empty(struct oda** ptr_tab)
{
    int i, j;   
    
    for (i = 0; i < X; i++) {
        for (j = 0; j < Y; j++) {
            ptr_tablo[i][j] = NULL;
        }
    }
}

But this is not working, i have:

Cannot assign a value of type void * to an entity of type struct oda

Can you help me please?

I played with *'s but i can't understand what can i do more it seems correct for me but not working

  • 1
    `struct oda* tab[X][Y];` means a 2-d array of pointers to structs, not a pointer to a 2-d array of structs. – Barmar Dec 07 '22 at 20:43
  • Yes, this is 2-d array of pointers to structs, each 2-d array of pointers should be NULL when i execute init_tab_empty. And i will create pointer value for each product placed on this table who point to our struct oda with another fontion called init_product. Example: tab[3][4] is empty tab[3][4] = NULL, id tab[3][4] have a product, tab[3][4] = (struct oda*)malloc(X * Y * sizeof(tab[X][Y])); – Musa Tombul Dec 07 '22 at 20:49
  • 1
    `int** ptr_tab` is not the same as `int ptr_tab[X][Y]`. The double pointer doesn't have information about the row width. – Barmar Dec 07 '22 at 20:53
  • 1
    you can't pass a 2-d array to a `TYPE **` parameter. I know there are past questions about this, but I don't have a link. – Barmar Dec 07 '22 at 20:55
  • i am calling fonction like this: init_tab_empty(&tab); , what i need to do if my method is false? – Musa Tombul Dec 07 '22 at 20:57
  • Why does the function say `int **` instead of `struct oda**`? – Barmar Dec 07 '22 at 20:57
  • i have already using: void init_tab_empty(struct oda** ptr_tab) but i have same result, corrected on thread – Musa Tombul Dec 07 '22 at 21:02

1 Answers1

0

If I understood you well, you want to pass a table to your function. Then the following change will make it okay:

void init_tab_empty(struct oda ***tab)
{
    int i, j;   
    
    for (i = 0; i < DIM_X; i++) {
        for (j = 0; j < DIM_Y; j++) {
            tab[i][j] = NULL;
        }
    }
}

This tells the function that you will input a table of pointers to struct oda data type.

There, you have:

struct oda *my_tab[DIM_X][DIM_Y];
...
init_tab_empty(my_tab);
Jib
  • 1,334
  • 1
  • 2
  • 12