1

I know there are some posts just like this one, but all solutions on those posts didn't help me, and they tended to just say switching l.valor to l->valor, and that is not working in my case, and I don't understand why.

This is what I have:

typedef struct lligada {
    int valor;
    struct lligada *prox;
} *LInt;

void insertOrd (LInt *g, int x){
    if (g != NULL) g->valor = x;
}

And I am getting this:

error: request for member 'valor' in something not a structure or union if (g == NULL) g->valor = x;

What am I doing wrong?

PS: This is an exercise on a platform called codeboard, it is homework, I can't change the struct declaration, nor can I change the arguments of the function insertOrd, I have to use what I get on insertOrd and make the function work

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
NepsName
  • 149
  • 1
  • 5
  • 11
  • 1
    1) `l` is pointer to pointer of `struct lligada` – BLUEPIXY May 22 '17 at 20:15
  • 2
    `l` is too easily confused with `1` in a monospaced font, and is the worst possible use of the single-letter variable. And the names of function arguments should *help* the user not hinder them. – Weather Vane May 22 '17 at 20:20
  • I've changed it to avoid confusion but it wasn't set by me, I just have to complete it. – NepsName May 22 '17 at 20:21
  • 3
    `(*g)->valor` etc. Pointers hidden in `typedefs` are confusing if you need to apply a dereference operation to it. – Jonathan Leffler May 22 '17 at 20:21
  • [Is it a good idea to typedef pointers?](https://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) – Lundin May 23 '17 at 08:22

2 Answers2

4

Remove the * from the struct declaration:

typedef struct lligada {
    int valor;
    struct lligada *prox;
} LInt;

By the way you're NULL check is inversed. It should probably read if (l != NULL).

nucleon
  • 1,128
  • 1
  • 6
  • 19
  • Ok, I should probably have said this already. This is an exercise on a platform called codeboard, it is homework, I can't change the struct declaration, nor can I change the arguments of the function insertOrd, I have to use what I get on insertOrd and make the function work – NepsName May 22 '17 at 20:18
  • @NepsName you can do whatever you like in your preparation of the homework, before you get to its requirement. – Weather Vane May 22 '17 at 20:35
1

Either you have to write

void insertOrd (LInt *g, int x){
    if (*g != NULL) ( *g )->valor = x;
        ^^ ^^         ^^^
}

or

void insertOrd (LInt g, int x){
                ^^^^^^
    if (g != NULL) g->valor = x;
          ^^
}

because the type of the parameter g in the presented function is struct lligada **

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I think this worked, but I have a problem with something else, how do I check if the LInt *g I receive as an argument is empty? I am supposed to make (*g)->valor = x if it is empty, but doing if((*g)==NULL) is giving me segmentation fault – NepsName May 22 '17 at 20:25
  • @NepsName `if (g != NULL && *g != NULL)` – BLUEPIXY May 22 '17 at 20:30
  • @NepsName I wrote in the answer that you have to check if ( *g != NULL ) instead of if ( *g == NULL ) – Vlad from Moscow May 22 '17 at 20:31