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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-21-2009, 10:40 AM
|
#1
|
|
LQ Newbie
Registered: Jun 2009
Posts: 4
Rep:
|
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;
}
|
|
|
|
06-21-2009, 11:13 AM
|
#2
|
|
Member
Registered: Jun 2009
Distribution: slackware
Posts: 123
Rep:
|
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.
|
|
|
|
06-21-2009, 11:47 AM
|
#3
|
|
Moderator
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,505
Rep: 
|
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.
|
|
|
|
06-21-2009, 04:20 PM
|
#4
|
|
LQ Newbie
Registered: Jun 2009
Posts: 4
Original Poster
Rep:
|
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.
|
|
|
|
06-21-2009, 04:49 PM
|
#5
|
|
Senior Member
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,669
|
Quote:
Originally Posted by sweetymobil
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.
|
|
|
|
06-21-2009, 05:04 PM
|
#6
|
|
Senior Member
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,669
|
Quote:
Originally Posted by noctilucent
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.
|
|
|
|
06-22-2009, 07:12 AM
|
#7
|
|
Member
Registered: Jun 2009
Distribution: slackware
Posts: 123
Rep:
|
Quote:
Originally Posted by johnsfine
It doesn't look reasonable to me.
|
The question wasn't addressed to you, so what is your point?
Quote:
Originally Posted by johnsfine
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.
|
|
|
|
06-22-2009, 07:46 AM
|
#8
|
|
Senior Member
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,669
|
Quote:
Originally Posted by noctilucent
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
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:56 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|