0

I defined a structure called coordonnees declared as a pointer, I want to append values to the pointer a_visiter but it's not working. How can I fix that?

Here is the struct code:

typedef struct couple
{
    int ligne;
    int colonne;
}*coordonnees;

int main()
{
    coordonnees a_visiter;
    a_visiter=(coordonnees)malloc(DIMENSION*4*sizeof(struct couple));
    int p=0;
etc ...
}
void voisin_egaux(char matrice[][DIMENSION+1],int x, int y,coordonnees **a_visiter,int *p)
{
    if (matrice[x][y]==matrice[x+1][y]){
        (a_visiter+(*p))->ligne=x+1;
        (a_visiter+(*p))->colonne=y;
         ++(*p);
        }

I get as an error: request for member ligne in something not a structure or union.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Elas
  • 21
  • 2

1 Answers1

0

This parameter declaration

coordonnees **a_visiter

is equivalent to

struct couple ***a_visiter

Thus the expression

(a_visiter+(*p))

has the above pointer type that does not point to an object of the structure type.

To make this code semantically valid

(a_visiter+(*p))->ligne=x+1;
(a_visiter+(*p))->colonne=y;

the parameter should be declared like

coordonnees a_visiter

Otherwise you need to write

( **a_visiter+(*p))->ligne=x+1;
( **a_visiter+(*p))->colonne=y;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I passed the coordoonnees a_visiter by reference because I will use it in another function so I should declare it as coordonnees *a_visiter or coordoonnees a_visiter ? – Elas Feb 10 '23 at 20:57
  • @Elas If you want to pass a pointer itself to an object of the structure type by reference then you should declare the parameter like coordonnees *a_visiter. If you want to pass an object of the structure type by reference then you should write coordonnees a_visiter. – Vlad from Moscow Feb 10 '23 at 21:08
  • @Elas It really depends on the use-case: Do you need to assign to `a_visiter`, to change where the original pointer is pointing? If not, then just plain `coordoonnees a_visiter`. But I really recommend you read the comment by Barmar: Don't hide pointers behind a type-alias. – Some programmer dude Feb 10 '23 at 21:11