Dereferencing pointer to incomplete type

Dereferencing pointer to incomplete type

Post by Georgi » Tue, 04 Mar 2003 19:53:02



Im fairly new to C etc, be kind!

In my header file I have:

#ifndef __STRINGTABLE__
#define __STRINGTABLE__
typedef struct StringTable *StringTable_ref;
#endif

In my main file I have:

#ifndef  __STRINGTABLE__
#define  __STRINGTABLE__
typedef struct StringTable {
       StringNode_ref   table;
       int              hsize;
       int           elements;

Quote:} StringTable_ref;

#endif

StringTable_ref new_StringTable( void ) {

     StringTable_ref table1 = malloc(sizeof(StringTable_ref));
     table1->table = malloc(sizeof(StringNode_ref) * 101);
     table1->hsize = 101;
     return table1;

Quote:}

And I am getting the error:
     Dereferencing pointer to incomplete type

At the lines table1->table... and table1->hsize...

I have been specifically told not to put my structures in my header
file. Any idea what I am doing wrong and what I can do?

Georgie

 
 
 

Dereferencing pointer to incomplete type

Post by Jens.Toerr.. » Tue, 04 Mar 2003 22:40:23



> Im fairly new to C etc, be kind!
> In my header file I have:
> #ifndef __STRINGTABLE__
> #define __STRINGTABLE__
> typedef struct StringTable *StringTable_ref;
> #endif
> In my main file I have:
> #ifndef  __STRINGTABLE__
> #define  __STRINGTABLE__
> typedef struct StringTable {
>        StringNode_ref   table;
>        int              hsize;
>        int           elements;
> } StringTable_ref;
> #endif

The way you use the #ifndef stuff there are two scenarios: If you
include the header file first, the typedef in the main file never
gets seen by the compiler. If you include the header file after the
typedef in the main file the contents of the header file never get
seen by the compiler. Having include protections like this in a
C file is usually not correct.

I guess you out this in because you got an error message when you didn't.
That's because without it you typedef StringTable_ref twice (and even
to different types, in the header file as a pointer to a StringTable
structure and in the main file as a StringTable structure. But you
can't have it both ways...

Quote:> StringTable_ref new_StringTable( void ) {
>      StringTable_ref table1 = malloc(sizeof(StringTable_ref));
>      table1->table = malloc(sizeof(StringNode_ref) * 101);
>      table1->hsize = 101;
>      return table1;
> }
> And I am getting the error:
>      Dereferencing pointer to incomplete type

It looks as if you include the header file first. Now the compiler
knows about StringTable_ref, but not anything more than that it
is a pointer to some kind of structure. What it does not know is
how the structure looks like, i.e. it doesn't has any idea what
kind of members the structure has. That's why you get the error
message.

But the whole thing looks rather broken. In the first place I would
not use additional pointer types like StringTable_ref, it only
tends to make things less comprehensible. When I see a '*' in front
of something it's immediately clear it's a pointer, but with
'StringTable_ref' I always have to check if this is really a pointer
or something else. But YMMV.

So I would completely get rid of the header file and instead simply
use:

typedef struct StringTable StringTable;
typedef struct StringNode  StringNode;

struct StringNode {
...

Quote:};

struct StringTable {
        StringNode   *table;
        int          hsize;
        int          elements;

Quote:};

StringTable *new_StringTable( void )
{
     StringTable *table1 = malloc( sizeof *table );
     table1->table = malloc( 101 * sizeof *table1->table );
     table1->hsize = 101;
     return table1;

Quote:}

Perhaps, as long as you seem still to be struggling with the
differences between typedefs and structures you should not use
typedefs at all (some people even think that typedefs should
never used for the purpose of just giving structures a new name).
Without the typedef it would only require just a bit more of
typing but things might get clearer:

struct StringNode {
...

Quote:};

struct StringTable {
        struct StringNode *table;
        int                hsize;
        int                elements;

Quote:};

struct StringTable *new_StringTable( void )
{
     struct StringTable table1 = malloc( sizeof *table );
     table1->table = malloc( 101 * sizeof *table1->table );
     table1->hsize = 101;
     return table1;

Quote:}

BTW, when the table member of the StringTable structure is really
a pointer to a StringNode why do you call it table and not for
example node? Would probably make things easier to read. Of course,
you can call e.g. an airplane a car, but it would make it difficult
for other people to understand you ;-)

                                      Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring

 
 
 

Dereferencing pointer to incomplete type

Post by Jens.Toerr.. » Wed, 05 Mar 2003 01:17:57



> struct StringTable *new_StringTable( void )
> {
>      struct StringTable table1 = malloc( sizeof *table );

Sorry, just realized I left out a '*' here:

       struct StringTable *table1 = malloc( sizeof *table );

Quote:>      table1->table = malloc( 101 * sizeof *table1->table );

                                   Regards, Jens

--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring

 
 
 

Dereferencing pointer to incomplete type

Post by Erik Max Franci » Wed, 05 Mar 2003 05:00:44



> And I am getting the error:
>      Dereferencing pointer to incomplete type

> At the lines table1->table... and table1->hsize...

> I have been specifically told not to put my structures in my header
> file.

Sometimes this has value, but I don't see the benefit here.  The usual
reason this is done is for API writers to give their clients opaque
(incomplete) types, so they cannot manipulate the data structures
involved directly (well, without random bit poking).

Quote:> Any idea what I am doing wrong and what I can do?

Either remove the #ifndef ... #endif guards around the definition of the
struct type in the source file, or put the definition of the type in the
header file.  As it stands right now, you're guarding against its
definition, so the source file never sees it, and so (rightfully)
complains when you try to allocate an instance of it.

And don't use identifiers with starting with underscores or containing
double underscores for the guard macro names; they're reserved by the
implementation.

--

 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Strange is our situation here upon earth.
\__/ Albert Einstein
    Esperanto reference / http://www.alcyone.com/max/lang/esperanto/
 An Esperanto reference for English speakers.

 
 
 

Dereferencing pointer to incomplete type

Post by Georgi » Wed, 05 Mar 2003 13:43:49


Thank you very much for the help.

I should have explained in more detail. I am making a compiler and the
structures I have been talking about are not in the main files. I need
to hide the structures from the main file, and have been told I must not
have the structures in the header file.

Having said that, I am probably going to take your advise and stick
everything in the header file until I figure out what to do with them.

Thanks, you are a life saver

Georgina



>>Im fairly new to C etc, be kind!

>>In my header file I have:

>>#ifndef __STRINGTABLE__
>>#define __STRINGTABLE__
>>typedef struct StringTable *StringTable_ref;
>>#endif

>>In my main file I have:

>>#ifndef  __STRINGTABLE__
>>#define  __STRINGTABLE__
>>typedef struct StringTable {
>>       StringNode_ref   table;
>>       int              hsize;
>>       int           elements;
>>} StringTable_ref;
>>#endif

> The way you use the #ifndef stuff there are two scenarios: If you
> include the header file first, the typedef in the main file never
> gets seen by the compiler. If you include the header file after the
> typedef in the main file the contents of the header file never get
> seen by the compiler. Having include protections like this in a
> C file is usually not correct.

> I guess you out this in because you got an error message when you didn't.
> That's because without it you typedef StringTable_ref twice (and even
> to different types, in the header file as a pointer to a StringTable
> structure and in the main file as a StringTable structure. But you
> can't have it both ways...

>>StringTable_ref new_StringTable( void ) {

>>     StringTable_ref table1 = malloc(sizeof(StringTable_ref));
>>     table1->table = malloc(sizeof(StringNode_ref) * 101);
>>     table1->hsize = 101;
>>     return table1;

>>}

>>And I am getting the error:
>>     Dereferencing pointer to incomplete type

> It looks as if you include the header file first. Now the compiler
> knows about StringTable_ref, but not anything more than that it
> is a pointer to some kind of structure. What it does not know is
> how the structure looks like, i.e. it doesn't has any idea what
> kind of members the structure has. That's why you get the error
> message.

> But the whole thing looks rather broken. In the first place I would
> not use additional pointer types like StringTable_ref, it only
> tends to make things less comprehensible. When I see a '*' in front
> of something it's immediately clear it's a pointer, but with
> 'StringTable_ref' I always have to check if this is really a pointer
> or something else. But YMMV.

> So I would completely get rid of the header file and instead simply
> use:

> typedef struct StringTable StringTable;
> typedef struct StringNode  StringNode;

> struct StringNode {
> ...
> };

> struct StringTable {
>         StringNode   *table;
>         int          hsize;
>         int          elements;
> };

> StringTable *new_StringTable( void )
> {
>      StringTable *table1 = malloc( sizeof *table );
>      table1->table = malloc( 101 * sizeof *table1->table );
>      table1->hsize = 101;
>      return table1;
> }

> Perhaps, as long as you seem still to be struggling with the
> differences between typedefs and structures you should not use
> typedefs at all (some people even think that typedefs should
> never used for the purpose of just giving structures a new name).
> Without the typedef it would only require just a bit more of
> typing but things might get clearer:

> struct StringNode {
> ...
> };

> struct StringTable {
>         struct StringNode *table;
>         int                hsize;
>         int                elements;
> };

> struct StringTable *new_StringTable( void )
> {
>      struct StringTable table1 = malloc( sizeof *table );
>      table1->table = malloc( 101 * sizeof *table1->table );
>      table1->hsize = 101;
>      return table1;
> }

> BTW, when the table member of the StringTable structure is really
> a pointer to a StringNode why do you call it table and not for
> example node? Would probably make things easier to read. Of course,
> you can call e.g. an airplane a car, but it would make it difficult
> for other people to understand you ;-)

>                                       Regards, Jens

 
 
 

1. : dereferencing pointer to incomplete type

Hi,

I am trying to make a sense out of the following partial code:

        #include <dirent.h>
        dirent *dp;
        if (!strcmp(dp->d_name, ".")) continue;
                .
                :

And, the compiler (gcc-2.95 on an i386 Linux)) complained with the
following error message:

        dereferencing pointer to incomplete type

I am just trying to learn C and got stuck with such an error.  If you can
shed some lights in this matter, I certainly will appreicate it.

TIA.

2. Linux SCSI Programming HOWTO (part 2/2)

3. 2.5.5 -- filesystems.c:30: In function `sys_nfsservctl': dereferencing pointer to incomplete type

4. whoami and passwd problem

5. Dereferencing pointer to incomplete type

6. Invalidating Users (temporarily)

7. 2.5.18 -- build failure -- suspend.c:1052: dereferencing pointer to incomplete type

8. slow X and no sound

9. dereferencing pointer to incomplete type?

10. Solving "dereferencing pointer to incomplete type" error

11. "dereferencing pointer to incomplete type"

12. wu-ftpd.2.4 on linux compile error. ( dereferencing pointer to incomplete type)

13. Please help!! structure datatype scope (dereferencing pointer to incomplete type)