Connecting Tech Pros Worldwide Forums | Help | Site Map

dereferencing pointer to incomplete type

Newbie
 
Join Date: Jul 2006
Posts: 1
#1: Jul 10 '06
Hello everybody,

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



typedef struct
{
struct lieu* cont;
struct joueur* j;
struct creature* c;
}cas;

typedef struct
{
struct cas* grille[N][N];
int NBL,NBC,i,j;
struct joueurs* js;
}monde;


void init_monde(monde* pm)
{
...
(*(*pm).grille[i][j]).j=NULL; <= dereferencing pointer to incomplete type
...
}


can somebody please help me ?? :)

D_C D_C is offline
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 294
#2: Jul 10 '06

re: dereferencing pointer to incomplete type


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.
Newbie
 
Join Date: May 2007
Posts: 19
#3: May 7 '07

re: dereferencing pointer to incomplete type


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. }
Newbie
 
Join Date: May 2007
Posts: 19
#4: May 9 '07

re: dereferencing pointer to incomplete type


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...
Newbie
 
Join Date: Aug 2008
Posts: 1
#5: Aug 28 '08

re: dereferencing pointer to incomplete type


Thanks teach!

(this is extraneous text to make the 20 character minimum)
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#6: Aug 28 '08

re: dereferencing pointer to incomplete type


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.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,185
#7: Aug 28 '08

re: dereferencing pointer to incomplete type


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 (->)?
Reply