Error importing structs from a header file

Asked

Viewed 337 times

1

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

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 contents placed in its entry 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;
                      ^

The initialization of 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;
}

At first I had thought that the structures were not actually being imported from the archive. h, but if so the compiler would already give me error in trying to declare the same before the for. So I believe I’m not assigning something necessary at some point within the function init_t, someone can see what’s wrong?

  • found this related answer, https://forum.zwame.pt/threads/duvida-programcao-error-dereferencing-pointer-incomplete-type.493621/ but still, I can’t quite fit the problem.

1 answer

1


What happens is that when you have a pointer and you take a position on that pointer, it ceases to be a pointer and becomes a variable. In other words, the pointer stores the memory address, when you pass the position, you’re already picking up the value from within the pointer, then the -> becomes . just like using on 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 variable, struct declaration.

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

} hash_t;

he of the conflict why entrada_hash_t is not a struct.

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

  • yes but this was not just one of the mistakes

  • @pmargreff changed the answer, see if it solves your problem

  • That’s right, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.