0

I do not understand why I get this error. I tried google but i think that my code seems right... In main I have this:

Database *database;
database = (Database *)malloc(db_sizeOfStruct());

database->key = "word";

And in my module file I have this:

typedef struct database {
     char key;
     char value;
     struct database *next;
} Database;

int db_sizeOfStruct() {
    return sizeof(struct database);
}

The compiler gives "dereferencing pointer to incomplete type", why? I am trying to understand pointers, it's propably something about them I suppose...

rablentain
  • 6,641
  • 13
  • 50
  • 91

5 Answers5

6

If the code in main() doesn't have access to the declaration of the struct, you can't use it like you do.

Either make the declaration public (in the module.h) or make the allocation inside the module.

Also you can't allocate a string (char *) to a single char, but that's a different error message.

Also, please don't cast the return value of malloc() in C.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
1

The problem is key, which is a char. You are trying to assign a pointer to char to a char.

mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32
  • That's not the reason for the error message quoted by the OP. – alk Nov 22 '13 at 12:28
  • Ah! Now I see. The char * -> char error was so obvious that I thought it was the cause for the error message. Anyway, a slightly more detailed comment would be better. Just a "that's not the answer" or "that's not the reason" isn't quite helpful. – mcleod_ideafix Nov 22 '13 at 12:31
  • There are correct answers around. If felt it wasn't necessary to just repeat those. And it's also always a good idea to read and understand the error message. In this case it was mentioning the word "dereference" which should norrow down the issue quiet well. – alk Nov 22 '13 at 12:33
1

Since this compiles pefectly:

#include <stdlib.h>

typedef struct database {
    char key;
    char value;
    struct database *next;
} Database;

int db_sizeOfStruct() {
    return sizeof(struct database);
}

int _tmain(int argc, _TCHAR* argv[])
{
    Database *database;
    database = (Database *)malloc(db_sizeOfStruct());

    database->key = 'w';//"word";

    return 0;
}

I assume you have this in a separate file that is unknown to main:

typedef struct database {
    char key;
    char value;
    struct database *next;
} Database;

int db_sizeOfStruct() {
    return sizeof(struct database);
}

Then this main have no idea what Database is:

int _tmain(int argc, _TCHAR* argv[])
{
    Database *database;
    database = (Database *)malloc(db_sizeOfStruct());

    database->key = 'w';//"word";

    return 0;
}

Also, see the assigning of "word" to a char, which obviously cannot be on purpose.

Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
  • You are right in that there is two separate files. The file containing the struct also has an header file where the typedef is defined as "typedef struct database Database;". I think that the type Database would be known in main then? – rablentain Nov 22 '13 at 12:28
0

Key is of type char but "word" is a string literal what means it has type char *

dhein
  • 6,431
  • 4
  • 42
  • 74
0

Your structure definition should look like:

typedef struct database {
     char *key;
     char value;
     struct database *next;
} Database;

Look at

char *key;

Your previous member was:

char key;

Just a single character that cannot hold a pointer to the "word" string.

Anyway, I have never seen using a function to return a size of some type.

malloc(sizeof(struct database));

is ugly?

mondrasovic
  • 170
  • 9