Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

297
Vistas
Why am I getting a "lvalue required as unary & operand" error?

I'm working on a function that inserts a value in a binary search tree, I wrote this code:

// this is my decalarations if its relevent
typedef struct element* tree;
typedef struct element
{  
    type data;
    tree right;
    tree left;
} noed;

tree rightSon(tree head)
{
    return (head->right);
}

void insert(tree* a,int val)
{
     if(!empty(*a)) {
         if((*a)->data>val) {    
             if(!empty(leftSon(*a)))
                  insert(&leftSon(*a),val); // error here leftson return a tree
            else {
             (*a)->left=newNoed(val);
            }
         }
         else if((*a)->data<val) {
              if(!empty(rightSon(*a)))
                  insert(&rightSon(*a),val); //same error here
            else 
             (*a)->right=newNoed(val);
         }
         else printf("value already exist!\n");
    }
    else {
        *a=newNoed(val);
    }
}

I solved the problem by declaring local variables of type tree then assigning the values to them like this:

tree lson;
lson=leftson(*a);
insert(&lson,val);

But I still don't get what was the problem in the first place.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

It seems the function leftSon (and rightSon) has the return type tree. That is it returns a temporary object. You may not apply the operator & to a temporary object like in this statement

insert(&leftSon(*a),val);

You could write the code without calling the functions leftSon or rightSon like

 if(!empty(rightSon(*a)))
     insert( &(*a)->right ,val);

In fact the if-else statements like this

         if(!empty(leftSon(*a)))
              insert(&leftSon(*a),val); // error here leftson return a tree
        else 
        {
         (*a)->left=newNoed(val);
        
        }

are redundant. Instead of them you could just write

insert( &(*a)->left, val );

and

insert( &(*a)->right, val );
over 4 years ago · Santiago Trujillo Denunciar

0

Your rightSon function is returning the value of the pointer object stored at head->right. You can't take the address of a value.

What you want to do instead is have rightSon return the address of head->right which can then be passed directly to insert.

So change rightSon to return the address:

tree *rightSon(tree head)
{
    return &head->right;
}

And call it like this:

insert(rightSon(*a),val); 

leftSon presumably has the same issue, so make a similar change for that.

Also, hiding a pointer behind a typedef is considered bad practice as it's no longer obvious by just looking at the code that a variable of that type is a pointer and can cause confusion to the reader.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda