By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

dereferencing pointer to incomplete type

P: 1
Hello everybody,

I'm writing a game program, and i have compilations errors.. :confused:



Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3.     struct lieu* cont;
  4.     struct joueur* j;
  5.     struct creature* c;
  6. }cas;
  7.  
  8. typedef struct
  9. {
  10.     struct cas* grille[N][N];
  11.     int NBL,NBC,i,j;
  12.     struct joueurs* js;
  13. }monde;
  14.  
  15.  
  16. void init_monde(monde* pm)
  17. {
  18. ...
  19.       (*(*pm).grille[i][j]).j=NULL;  <= dereferencing pointer to incomplete type  
  20. ...
  21. }
  22.  
  23.  
can somebody please help me ?? :)
Jul 9 '06 #1
Share this Question
Share on Google+
7 Replies


100+
P: 293
D_C
I had a similar problem earlier, and fixed it by:
Expand|Select|Wrap|Line Numbers
  1. void foo(data* ptr)
  2. {
  3.    data temp = ptr;
  4.    ...
  5. // Do operation(s) on temp
  6.    ...
  7. // Then point ptr to the updated version of itself
  8.    ptr = &temp;
  9. }
I'm not sure if the * and & are in the right places, but let me know if you don't understand the idea.
Jul 10 '06 #2

P: 19
Had the same issue half hour ago; the temporary rid me of "dereferencing pointer to incomplete type", but its declaration spits out another error: "storage size of 'tmp' isn't known". Full code:
Expand|Select|Wrap|Line Numbers
  1. typedef unsigned int uint;
  2. typedef struct _Linie{
  3.   char *line;         // 4
  4.   uint length, alloc; // 8
  5.   struct Linie *next, *prev; // 8
  6. }Linie; // =20.
  7.  
  8. void init( struct Linie* linie, uint size ){
  9.   if( linie==NULL ) linie = (struct Linie*)malloc( sizeof( Linie) );
  10.  
  11.   struct Linie l; // <= storage size of 'l' isn't known
  12.   l.line = (char*)malloc( sizeof(char)*size );
  13.   l.alloc = size;  l.length = 0;  l.next = l.prev = NULL;
  14.  
  15.   linie = &l; // works... well, ok, i don't get an error!
  16.   //*linie = l; // I get the "dereferencing..." error again.
  17. }
  18. int main(){
  19.   struct Linie *primul;
  20.   primul = (struct Linie*)malloc( sizeof( Linie) );
  21.   init( primul, 20 );
  22.  // with 'init' commented out, prints the expected 20.
  23.   printf( "\n%d\n", sizeof(Linie) );
  24.   return 0;
  25. }
May 7 '07 #3

P: 19
Teacher solved the issue for me. All I had to do is remove "struct" from the function declaration:
Expand|Select|Wrap|Line Numbers
  1. void init( struct Linie* linie, uint size ){...}
and no more problems.
For the heck of it, I tried removing the temporary entirely:
Expand|Select|Wrap|Line Numbers
  1. void init(Linie* l, uint size ){
  2.   if(l==NULL ) l = (Linie*)malloc( sizeof( Linie) );
  3.   l->line = (char*)malloc( sizeof(char)*size );
  4.   l->alloc = size;
  5.   l->length = 0;
  6.   l->next = l->prev = NULL;
  7. }
Now that works as well...
May 9 '07 #4

P: 1
Thanks teach!

(this is extraneous text to make the 20 character minimum)
Aug 27 '08 #5

Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. void init_monde(monde* pm)
  2. {
  3. ...
  4.       (*(*pm).grille[i][j]).j=NULL;// <= dereferencing pointer to incomplete type
  5. ...
  6. }
[/quote]

Not sure, but I think the problem is that the dereference operator (*) takes higher precedence than the member access operator (.) - that is, you should be writing:

Expand|Select|Wrap|Line Numbers
  1. *((*pm).grille[i][j]).j = NULL;
In the code you have, you are trying to dereference *pm, which would be of type monde. Since monde is not a pointer, you can't dereference it.
Aug 28 '08 #6

Banfa
Expert Mod 5K+
P: 8,916
It should be noted that the use of the temporary variables in all the examples above actually causes the functions to do nothing or at least have rather strange effects depending on presence or not of copy constructors.

The init function of post 4 actually causes a memory leak there is little code in this thread so far of high quality and I would not copy any of it.

The normal resolution of an incomplete type is to include the header defining the type.

Rather than worry about the relative precedences of the dereference operator (*) and member access operator (.) why not just use the member access by pointer operator (->)?
Aug 28 '08 #7

P: 1
I know it's a bit late, but as it's still on the internet it can help other who look for the right answer.

The problem is with typedef struct {} name; structure.

If you define some type based on structure then you use this defined type WITHOUT struct keyword.

So in the example sent by Vivi I would define monde as:
Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3.     struct lieu* cont;
  4.     struct joueur* j;
  5.     struct creature* c;
  6. }cas;
  7.  
  8. typedef struct
  9. {
  10.     cas* grille[N][N]; /* <= changed line */
  11.     int NBL,NBC,i,j;
  12.     struct joueurs* js;
  13. }monde;
  14.  
Btw. I think, that it's good practice to add name to the structure type (since now it's anonymus), just like Strika Amaru did in his piece of code. In this example it would be:
Expand|Select|Wrap|Line Numbers
  1. typedef struct _tagcas /* <= changed */
  2. {
  3.     struct lieu* cont;
  4.     struct joueur* j;
  5.     struct creature* c;
  6. }cas;
  7.  
  8. typedef struct _tagmonde /* <= changed */
  9. {
  10.     cas* grille[N][N]; /* <= or struct _tagcas* ... */
  11.     int NBL,NBC,i,j;
  12.     struct joueurs* js;
  13. }monde;
  14.  
And I agree with Banafa that code below looks much better:
Expand|Select|Wrap|Line Numbers
  1. pm->grille[i][j]->j=NULL;
  2.  
Regards,
Paweł
Aug 20 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.