Foros del Web » Programación para mayores de 30 ;) » Programación General »

error con una variable no declarada que si lo está

Estas en el tema de error con una variable no declarada que si lo está en el foro de Programación General en Foros del Web. Hola a todos, tengo el siguiente código y al compilar me dice que en la funcion create_hash_table `hash_value_t' undeclared (first use in this function). Si ...
  #1 (permalink)  
Antiguo 03/05/2005, 09:49
 
Fecha de Ingreso: diciembre-2003
Mensajes: 190
Antigüedad: 20 años, 5 meses
Puntos: 0
error con una variable no declarada que si lo está

Hola a todos, tengo el siguiente código y al compilar me dice que en la funcion
create_hash_table `hash_value_t' undeclared (first use in this function). Si se supone que es global no entiendo porque dice queno está declarada.

Gracias a todos. Un saludo



#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>


typedef struct _list_t_ {
char *str;
struct _list_t_ *next;
} list_t;



typedef struct _hash_table_t_ {
int size; /* the size of the table */
list_t **table; /* the table elements */
} hash_table_t;



//prototipos

void free_table(hash_table_t *hashtable);
int add_string(hash_table_t *hashtable, char *str);
list_t *lookup_string(hash_table_t *hashtable, char *str);
unsigned int hash(hash_table_t *hashtable, char *str);
hash_table_t *create_hash_table(int size);

//fin prototipos



int main ()

{

hash_table_t *my_hash_table;
int size_of_table = 12;
my_hash_table = create_hash_table( size_of_table);

return 0;
}




hash_table_t *create_hash_table( int size)
{
hash_table_t *new_table;

int i;

if (size<1) return NULL; /* invalid size for table */

/* Attempt to allocate memory for the table structure */
if ((new_table = malloc(sizeof(hash_value_t))) == NULL) {
return NULL;
}

/* Attempt to allocate memory for the table itself */
if ((new_table->table = malloc(sizeof(list_t *) * size)) == NULL) {
return NULL;
}

/* Initialize the elements of the table */
for(i=0; i<size; i++) new_table->table[i] = NULL;

/* Set the table's size */
new_table->size = size;

return new_table;
}


unsigned int hash(hash_table_t *hashtable, char *str)
{
unsigned int hashval;

/* we start our hash out at 0 */
hashval = 0;

/* for each character, we multiply the old hash by 31 and add the current
* character. Remember that shifting a number left is equivalent to
* multiplying it by 2 raised to the number of places shifted. So we
* are in effect multiplying hashval by 32 and then subtracting hashval.
* Why do we do this? Because shifting and subtraction are much more
* efficient operations than multiplication.
*/
for(; *str != '\0'; str++) hashval = *str + (hashval << 5) - hashval;

/* we then return the hash value mod the hashtable size so that it will
* fit into the necessary range
*/
return hashval % hashtable->size;
}


list_t *lookup_string(hash_table_t *hashtable, char *str)
{
list_t *list;
unsigned int hashval = hash(hashtable, str);

/* Go to the correct list based on the hash value and see if str is
* in the list. If it is, return return a pointer to the list element.
* If it isn't, the item isn't in the table, so return NULL.
*/
for(list = hashtable->table[hashval]; list != NULL; list = list->next) {
if (strcmp(str, list->str) == 0) return list;
}
return NULL;
}


int add_string(hash_table_t *hashtable, char *str)
{
list_t *new_list;
list_t *current_list;
unsigned int hashval = hash(hashtable, str);

/* Attempt to allocate memory for list */
if ((new_list = malloc(sizeof(list_t))) == NULL) return 1;

/* Does item already exist? */
current_list = lookup_string(hashtable, str);
/* item already exists, don't insert it again. */
if (current_list != NULL) return 2;
/* Insert into list */
new_list->str = strdup(str);
new_list->next = hashtable->table[hashval];
hashtable->table[hashval] = new_list;

return 0;
}



void free_table(hash_table_t *hashtable)
{
int i;
list_t *list, *temp;

if (hashtable==NULL) return;

/* Free the memory for every item in the table, including the
* strings themselves.
*/
for(i=0; i<hashtable->size; i++) {
list = hashtable->table[i];
while(list!=NULL) {
temp = list;
list = list->next;
free(temp->str);
free(temp);
}
}

/* Free the table itself */
free(hashtable->table);
free(hashtable);
}
  #2 (permalink)  
Antiguo 03/05/2005, 09:56
 
Fecha de Ingreso: abril-2005
Mensajes: 3.083
Antigüedad: 19 años
Puntos: 17
Es que no está declarada...

hash_value_t no existe. En tal caso hash_table_t.

Por cierto, no vuelvas a postear códigos tan largos que no los leeremos. Copia las funciones donde falle y si necesitamos más lo pediremos.

Lo que también creo es que ese código le has pillado de algún lado y pusieron el error a propósito (como se suele hacer), para evitar que lo usen novatos.
  #3 (permalink)  
Antiguo 03/05/2005, 10:17
 
Fecha de Ingreso: diciembre-2003
Mensajes: 190
Antigüedad: 20 años, 5 meses
Puntos: 0
ok, perdona solo lo puse para que tuvieras una vision mas global.
Por cierto, tengo un problema con esta funcion donde el segundo FOR me da un error del tipo "error: incompatible types in assignment" y en el primer FOR
"statement with no effect"

Podrias echarme una mano?


int get_all_strings(hash_table_t *hashtable)
{
int i, count = 0;
list_t *list;

/* error check to make sure hashtable exists */
if (hashtable==NULL) return -1;

/* go through every index and count all list elements in each index */
for(i=0; i<hashtable->size; i)
{
for(list=hashtable[i]; list != NULL; list = list->next)
{
printf ("el valor: %sºn", list->str);
}


}

return count;
}
  #4 (permalink)  
Antiguo 03/05/2005, 11:54
 
Fecha de Ingreso: diciembre-2002
Mensajes: 23
Antigüedad: 21 años, 4 meses
Puntos: 0
El error (mas bien advertencia) en el primer for es causado por la parte de incremento ya que no incrementas la variable 'i' y el error en el segundo for se debe a que no estas accediendo correctamente al miembro 'table' de la estructura.

Un saludo
  #5 (permalink)  
Antiguo 03/05/2005, 11:55
Avatar de Eternal Idol  
Fecha de Ingreso: mayo-2004
Ubicación: Lucentum
Mensajes: 6.192
Antigüedad: 20 años
Puntos: 74
A vuelo de pajaro:
for(i=0; i<hashtable->size; i) << i++

list=hashtable[i]; << list_t no es hash_table_t
__________________
¡Peron cumple, Evita dignifica! VIVA PERON CARAJO
  #6 (permalink)  
Antiguo 03/05/2005, 12:03
 
Fecha de Ingreso: abril-2005
Mensajes: 3.083
Antigüedad: 19 años
Puntos: 17
Empieza a sonarme a tarea escolar de correción de errores...
  #7 (permalink)  
Antiguo 03/05/2005, 12:24
 
Fecha de Ingreso: diciembre-2003
Mensajes: 190
Antigüedad: 20 años, 5 meses
Puntos: 0
No entiend pq se accede mal a la estrucutra, el primer argumento del for es asignar al puntero list la posicion que altualmente tiene la tabla hash y mientras no sea null avanzar al siguiente nodo de la lista enlazada. ¿no?
  #8 (permalink)  
Antiguo 03/05/2005, 12:29
Avatar de Eternal Idol  
Fecha de Ingreso: mayo-2004
Ubicación: Lucentum
Mensajes: 6.192
Antigüedad: 20 años
Puntos: 74
¿Es una pregunta? Deberias saber perfectamente lo que hace el codigo ...

Proba con: for(list=*hashtable[i].table; list != NULL; list = list->next)
__________________
¡Peron cumple, Evita dignifica! VIVA PERON CARAJO
  #9 (permalink)  
Antiguo 03/05/2005, 12:34
 
Fecha de Ingreso: diciembre-2003
Mensajes: 190
Antigüedad: 20 años, 5 meses
Puntos: 0
vamos a ver, si ambos son punteros, no puedo pasar la direccion de un puntero de una variable a otra?
q es un casting? es una conversion de tipos?
  #10 (permalink)  
Antiguo 03/05/2005, 12:36
Avatar de Eternal Idol  
Fecha de Ingreso: mayo-2004
Ubicación: Lucentum
Mensajes: 6.192
Antigüedad: 20 años
Puntos: 74
No y ademas no tiene sentido porque el hash_table_t tiene una lista como miembro, algo que no habia visto antes, y estarias apuntando a size, el primer miembro.

Si, un casting es una conversion de tipos, pero no aplica a este caso, mira mi anterior mensaje que lo edite.
__________________
¡Peron cumple, Evita dignifica! VIVA PERON CARAJO
  #11 (permalink)  
Antiguo 03/05/2005, 12:55
 
Fecha de Ingreso: diciembre-2003
Mensajes: 190
Antigüedad: 20 años, 5 meses
Puntos: 0
Esto se supone que deberia de ser una tabla dinamica y por cada hueco lleva asociado una lista enlazada, es decir, se trata de una tabla hash, pero estoy probando en codigo y veo q no hace lo que deberia de hacer ya que he insertado 3 valores y solo me muestra uno. Por cierto, muchas gracias por tu explicación.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:31.