LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-21-2009, 10:40 AM   #1
sweetymobil
LQ Newbie
 
Registered: Jun 2009
Posts: 4

Rep: Reputation: 0
dereferencing pointer to incomplete type


Error dereferencing pointer to incomplete type
in line: looptree cab = &firstNode; and subsequence use of the
pointer cab-> in function isThere.

What's wrong? Please help!
Thanks


#include <limits.h>
#include <stdlib.h>
#include <assert.h>

#define NUM_ELEMS 10000
#define TRUE 1
#define FALSE 0

typedef struct node *looptree;
typedef struct {
unsigned long long value;
looptree left;
looptree right;
}node;

int isThere(looptree cab, unsigned long long randomNumber);

int main () {

unsigned long long randomNumber; // 64 bits
randomNumber=123456789llu;

node firstNode;
looptree cab = &firstNode;
cab->left=NULL;
cab->right=NULL;
cab->value=randomNumber;

int counter = 0;
int foundLoop = FALSE;
while(!foundLoop){

randomNumber = randomNumber * randomNumber;
randomNumber = randomNumber / 100000llu;
randomNumber = randomNumber % 1000000000llu;

if (isThere(cab, randomNumber)){
printf("Loop at counter %d\n", counter);
foundLoop = TRUE;
}
printf("%llu\n", randomNumber);
counter++;
}

printf("\nFinish counter=%d\n", counter);
getchar();
return 0;
}

int isThere(looptree cab, unsigned long long key){
int isInTree = FALSE;
assert (cab !=NULL);

if (key == cab->value){
isInTree = TRUE;
} else if (key < cab->value){
if (cab->left == NULL){
cab->left = malloc(sizeof(node));
cab->left->value = key;
cab->left->left = NULL;
cab->left->right = NULL;
} else {
isInTree = isThere(cab->left, key);
}
}else {
if (cab->right == NULL){
cab->right = malloc(sizeof(node));
cab->right->value = key;
cab->right->left = NULL;
cab->right->right = NULL;
} else {
isInTree = isThere(cab->right, key);
}
}
return isInTree;
}
 
Old 06-21-2009, 11:13 AM   #2
noctilucent
Member
 
Registered: Jun 2009
Distribution: slackware
Posts: 123

Rep: Reputation: 25
In an attempt to make sure that.. I don't exactly know what (you type less? your code is somehow clearer?) you've managed to produce a code piece which does not build. Does the idea of staying away from typedefs (at least at this point) look reasonable to you? Consider this:

Code:
typedef struct node *looptree;
but! You've cleverly made sure that there is no such thing as a "struct node", it's just "node", because of this:

Code:
typedef struct { /* .. */ } node;
There are also a number of other [unrelated] things which are wrong with your code.
 
Old 06-21-2009, 11:47 AM   #3
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,505

Rep: Reputation: 115Reputation: 115
noctilucent is correct (and fairly blunt) about it. Your use of multiple typedefs has resulted in incomplete types.
Consider the following:
Code:
struct node_struct {
    unsigned long long value;
    struct node_struct *left;
    struct node_struct *right;
};
typedef struct node_struct node;
This will let you use node to refer to a single node, with a complete type defined when you need it.
 
Old 06-21-2009, 04:20 PM   #4
sweetymobil
LQ Newbie
 
Registered: Jun 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Hi and thank you! Actually I'm struggling with Tree structure in trying to insert a randomNumber if it's not in the tree yet going down left tree or the other.
Could you please point out my errs... "There are also a number of other [unrelated] things which are wrong with your code." I sure can use guidance from the experts.
Thanks again.... Oh by the way sorry I forgot to use indentation... Promise never to repeat this mistake.
 
Old 06-21-2009, 04:49 PM   #5
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,669

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
Quote:
Originally Posted by sweetymobil View Post
Code:
typedef struct node *looptree;
typedef struct {
        unsigned long long value;
        looptree left;
        looptree right;        
}node;
You defined looptree as a pointer to a struct node and then you defined what a node is, but you haven't actually defined what a struct node is.

As a long time C++ programmer, my C is too rusty to be exactly sure what your code means, but I'm sure it isn't what you intended it too mean.

I'm not certain (too spoiled by the cleaner way structs work in C++) but maybe

Code:
struct node;
typedef struct node *looptree;
typedef struct node {
        unsigned long long value;
        looptree left;
        looptree right;        
}node;
First predeclare that struct node will be defined later, so you can correctly define a pointer to it.
Then define that pointer,
Then define both struct node and node (using looptree).

C++ corrects that particular language design flaw of C.

Code:
struct node;  // predeclare the structure
typedef node *looptree;  // define the pointer type
struct node {            // define the structure, which is a type
        unsigned long long value;
        looptree left;
        looptree right;        
};

Last edited by johnsfine; 06-21-2009 at 05:21 PM.
 
Old 06-21-2009, 05:04 PM   #6
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,669

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
Quote:
Originally Posted by noctilucent View Post
Does the idea of staying away from typedefs (at least at this point) look reasonable to you?
It doesn't look reasonable to me.
typedefs are great.

struct node_struct *left; from Matir's example is OK, but
looptree left; from the original example is nicer.

The trick is how to get that predeclare correct so looptree can be defined before the struct but as a pointer to it.

In an example this simple, Matir's approach looks better, because it avoids the need for any pre declaration. But in the real world of serious projects with pointers to structures, the circularities are not so easily suppressed. The forward declarations will be required and using typedefs (rather than things like struct node_struct * ) leads to more readable maintainable code.

Quote:
There are also a number of other [unrelated] things which are wrong with your code.
I took a quick read through and didn't spot them.

Last edited by johnsfine; 06-21-2009 at 05:29 PM.
 
Old 06-22-2009, 07:12 AM   #7
noctilucent
Member
 
Registered: Jun 2009
Distribution: slackware
Posts: 123

Rep: Reputation: 25
Quote:
Originally Posted by johnsfine View Post
It doesn't look reasonable to me.
The question wasn't addressed to you, so what is your point?

Quote:
Originally Posted by johnsfine View Post
I took a quick read through and didn't spot them.
Once again: what's your point? Just a random piece of information? Look again, if you really care.

Last edited by noctilucent; 06-22-2009 at 07:15 AM.
 
Old 06-22-2009, 07:46 AM   #8
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,669

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
Quote:
Originally Posted by noctilucent View Post
The question wasn't addressed to you, so what is your point?
I think it is helpful to the OP to give an alternate opinion when I think the advice he already got was poor (just about avoiding typedef).

Quote:
Originally Posted by noctilucent View Post
Once again: what's your point? Just a random piece of information? Look again, if you really care.
I don't normally comment on the impoliteness of other people providing answers in a technical forum. I have trouble myself in resolving the frequent conflict between helpful and polite. But I think you are being impolite in the course of being intentionally less helpful. What's your point?

I think I'll wait for the follow up question from the OP before trying that hard to spot the next bug.

Last edited by johnsfine; 06-22-2009 at 07:48 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
tai64nlocal.c:55: error: dereferencing pointer to incomplete type ExCIA Linux - General 1 03-31-2009 09:49 AM
dereferencing pointer to incomplete type (just built a new kernel) 144419855310001 Linux - Kernel 0 10-03-2007 03:58 PM
error: dereferencing pointer to incomplete type ChullDouvre Programming 2 05-02-2007 12:16 AM
Error: dereferencing pointer to incomplete type cynthia_thomas Programming 1 05-01-2006 08:10 AM
C error "dereferencing pointer to incomplete type" lucs Slackware 6 02-21-2005 09:33 AM


All times are GMT -5. The time now is 10:56 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration