warning: passing argument 1 of 'enlist' from incompatible pointer type [enabled by default]

3

I try to implement a Hash in C. The program compiles, but in the file myHash.h pulls the following warning:

  

warning: passing argument 1 of 'enlist' from incompatible pointer type [enabled by default]

myHash.h file

#ifndef MYHASH_H_INCLUDED
#define MYHASH_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define HASHMAX 10

typedef struct nodoL {
    int info;
    struct nodoL * sig;
} * lista;


//Definición de las Funciones
// DEFINICIÓN DE LAS FUNCIONES
//************* HASH **********


typedef lista Hash[HASHMAX];
int HashClave(int n);
void HashInsertar(Hash *H, int e);
void HashMostrar(Hash h);
void HashBuscar(Hash h,int n);

//********  LISTA   *******
void enlistar (lista *L, int n);
int mostrar(lista L);/* muestra por pantalla los valores de L, en forma recursiva */

#endif // MYHASH_H_INCLUDED

All right.

File myHash.c

#include "myHash.h"

int HashClave ( int n)
{
    return n%HASHMAX;
}

void HashInsertar (Hash *H, int e){
    enlistar ((H)[HashClave(e)],e);
}

void enlistar (lista *L, int n){ //<--note: expected 'struct nodoL **' but argument is of type 'struct nodoL *'
        lista aux = (lista)malloc(sizeof( struct nodoL ));
        if(L==NULL){
           aux -> info = n;
           aux -> sig = *L;
           *L=aux;
      }
       else{
           if((*L)->info>n){// Para que quede ordenado
               aux -> info = n;
               aux -> sig = *L;
               *L=aux;
           }
           else{
               enlistar((*L)->sig,n); //<-- warning: passing argument 1 of 'enlistar' from incompatible pointer type [enabled by default]
           }
      }
}

int mostrar (lista L)
{
    int i=0;
    if(L!=NULL)
    {
        i=1;
        printf("[%d]->",L->info);
        mostrar(L->sig);
    }
    return i;
}

void HashMostrar(lista h){
    int i, n;
    for (n=0;n<HASHMAX;n++){
        printf("Hash [%d]: ",n);
        i=mostrar(*h);
        if(i==0)printf("Lista  vacia \n\n");
        else printf("# \n\n");
    }
}

void borrar(lista *L, char n)
{
    lista aux = *L; //puntero auxiliar al primer nodo
    lista ant = NULL;
    if(aux==NULL)
    {
        printf("Error: Lista vacia");
    }
    else
    {
        while (aux->info!=n&&aux->sig!=NULL)
        {
            ant=aux;
            aux=aux->sig;
        }
        if(aux->sig==NULL&&aux->info!=n)
            printf("Error, caracter no se encuentra en la lista");
        else
        {
            ant->sig=aux->sig;
            free (aux);   //elimino el nodo de la memoria
        }
    }
}

Additionally, pull the following note:

  

note: expected 'struct nodeL **' but argument is of type 'struct nodeL *' on the line marked.

What could be happening?

    
asked by Alejandro Caro 16.09.2017 в 22:03
source

1 answer

1

The warning is clear: you are passing a incorrect pointer to your enlistar( ) function.

void enlistar (lista *L, int n);

You expect a double pointer to a struct nodeL .

enlistar((*L)->sig,n);

You are dereferencing the pointer. As you point to a nodoL , you take your field struct nodoL * sig .

But ... that field is a simple pointer , and do not double.

Apart from that, your code does things rare :

lista aux = malloc( sizeof( lista ) );
  if( L == NULL ) {
    aux -> info = n;
    aux -> sig = *L;
    *L = aux;
  }
  ...

Create dynamic memory ... a pointer to a structure, not the structure itself. And, based on that pointer (which is uninitialized, and will point anywhere), you write values ... the result of that must be pretty curious : -)

I'm not sure what you intend to do when passing a double pointer as an argument. The solution is to change the problematic call:

enlistar( (*L).sig, n );

Do not dereference the pointer, but pass it as is .

EDITO

In the end, you have forced me to code a test case and everything; -)

#include <stdio.h>
#include <stdlib.h>

typedef struct nodoL {
  int info;
  struct nodoL * sig;
} *lista;

void enlistar( lista *L, int n ) {
  lista aux = (lista)malloc( sizeof( struct nodoL ) );

  aux->info = n;

  if( *L == NULL ) {
    aux->sig = NULL;
    *L = aux;
  } else {
    if( (*L)->info > n ) {
      aux->sig = *L;
      *L = aux;
    } else {
      enlistar( &(*L)->sig, n );
    }
  }
}

void mostrar( lista nodo ) {
  if( nodo ) {
    printf( "%p -> %d, %p\n", nodo, nodo->info, nodo->sig );
    mostrar( nodo->sig );
  }
}

int main( void ) {
  lista Lista = NULL;

  enlistar( &Lista, 4 );
  enlistar( &Lista, 2 );
  enlistar( &Lista, 5 );
  enlistar( &Lista, 1 );
  enlistar( &Lista, 3 );

  mostrar( Lista );

  return 0;
}

They leave 2 warnings in the call to printf( ) , but they can be safely ignored.

    
answered by 16.09.2017 / 23:28
source