Error importing structs from a header file

1

I need to use the following structures without changing their implementations from a .h file.

typedef unsigned long int chave_t;
typedef unsigned long int conteudo_t;

typedef struct entrada_hash{
    chave_t chave;
    conteudo_t * conteudo;
    struct entrada_hash * proximo;
} entrada_hash_t;

typedef struct elemento_lista{
    entrada_hash_t * elemento;
    struct elemento_lista * proximo;
} elemento_lista_t;


typedef struct tabela_hash{
    unsigned long int numero_elementos;
    struct entrada_hash_t * armazenamento;                
    void * sincronizacao; 

} hash_t;

However when initializing the hash table and trying to access any of the contents placed in your input I get the following error:

hash_s.c: In function ‘ht_init’:
hash_s.c:18:3: error: invalid use of undefined type ‘struct entrada_hash_t’
   hash->armazenamento[i]->chave = i;
   ^
hash_s.c:18:22: error: dereferencing pointer to incomplete type
   hash->armazenamento[i]->chave = i;
                      ^

Initializing the variables I do as follows:

hash_t * ht_init(unsigned long tam) {
    hash_t * hash ;
    unsigned long i = 0;
    hash = malloc(sizeof(hash_t));
    hash->numero_elementos = tam;
    hash->armazenamento = malloc (tam * sizeof(entrada_hash_t));
    for(i=0;i<tam;i++){
        hash->armazenamento[i]->chave = i;
    }
    return hash;
}

First I had thought that the structures were not being imported from the .h file, but if that were the way the compiler would already give me an error trying to declare them previously to for . So I guess I'm not assigning something needed at some point inside the init_t function, can anyone see what's wrong?

    
asked by anonymous 10.04.2016 / 18:37

1 answer

1

What happens is that when you have a pointer and take a position of that pointer, it stops being a pointer and becomes a variable. In other words, the pointer saves the memory address, when you pass the position, you are already getting the value from inside the pointer, so the -> becomes . as well as using it in a struct normal.

As in the example below:

#include <stdio.h>
#include <malloc.h>

typedef struct{
    int data;
}teste;

int caso1(){
    teste *t = (teste*) malloc(sizeof(teste));

    t->data = 5;

    printf("%d\n",t->data);
}

int caso2(){
    teste *t = (teste*) malloc(sizeof(teste));

    t[0].data = 5;

    printf("%d\n",t[0].data,);
}

int main(int argc, char **argv){
    caso1();
    caso2();

    return 0;
}

The other error is in the declaration of the variable, struct.

typedef struct tabela_hash{
    unsigned long int numero_elementos;
    struct entrada_hash_t * armazenamento;                
    void * sincronizacao; 

} hash_t;

of the conflict because entrada_hash_t is not a struct .

To resolve this, the armazenamento field must be of type struct entrada_hash or entrada_hash_t , which are the types of its structures.

    
10.04.2016 / 21:38