• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Quick Pointer Question in C

cchen

Diamond Member
if i have a function

void insert3 (node ***tree, char *word)

where tree is a bst, how would i call the function recursively on the left branch of the tree? I tried insert3(&(**tree)->left,word) but that gives me the error:

Error : illegal implicit conversion from 'struct node_s **' to
'struct node_s ***'
main.c line 127 insert3(&(**tree)->left,word);

 
If ptr is a pointer, then &(*ptr) have pointer operators that cancel out, which doesn't make sense IMO. If I had to guess based on the function signature alone, I would take out the address of operator.

But I'm not so familiar with BST or algorithms in C. Are you sure you need a node *** parameter for your function? Like I said, I'm a relative neophyte in C but I don't recall seeing 3 successive levels of pointers before.
 
yeah, you're not referencing the tree correctly.

First of all, why you're using a triple pointer for a BST is beyond me... a single pointer is sufficient. But to call it, I would just do, insert3(&((**tree)->left), word) that way you get the address of the actual "left" element of the double pointer tree.

oh yeah, and if you came up with the triple pointer structure (as opposed to your professor handing you code already in that format), then I highly suggest you rethink your structure.
 
hmm i tried that but it doesn't work - gives me the same error
i'm using a triple pointer because its being called from another function...
 
I can not think of a reason to use node ***. I don't think I've ever seen that before.

Basically in your function call you're passing the address of left. And it looks like left is defined as "struct node_s *" (according to the error). So you're passing "struct node_s **" but from your function declaration, the compiler is expecting "struct node_s ***". These are differing levels of dereference. You could try calling it like "insert3(&&(**tree)->left, word);" that would solve that problem (at that level) right away, but uhh.. your program is probably going to die a horrible death.. if the compiler even lets you..

The "&(**tree)->left" will return an address, and the next & will return the address of that which probably only exists on the stack. And anything you do with that pointer is only going to be bad news.

I believe your use of a *** is incorrect, but it would be hard to tell without seeing more code.
 
thanks... fixed that problem.....

one more quick question

void insert3 (node **tree, char *word) {

if ((*tree)==NULL) {
(*tree)=(node *)malloc(sizeof(tree));
(*tree)->left=NULL;
(*tree)->right=NULL;
(*tree)->word=(char *)malloc(100);
strcpy((*tree)->word,word);
return;
}
else {
if (strcmp(word,(*tree)->word)<0)
insert3(&(*tree)->left,word);
else {
if (strcmp(word,(*tree)->word)>0)
insert3(&(*tree)->right,word);
}
}
}

that's my insert function into the bst... if i insert words into the bst... after inserting the second word... the bst becomes messed up.... the words insert correctly... but say if i was only inserting to the left side, somehow the right side would be pointing to some random unallocated memory..... anyone see why that would be?
 
(*tree)=(node *)malloc(sizeof(tree));

That line is incorrect. tree is a node **. Since tree is a pointer, sizeof will return the size of a pointer. You will want to do sizeof(node).
 
your code is pretty wrong man.



your header if i were to do this i'd make node* tree





for the mallocs you do .


tree=(node*) malloc (sizof (struct node ) );


optionally


tree=(node*) malloc (sizeof (node * ) ) i'm told works as well.
 
Originally posted by: hans007
your code is pretty wrong man.



your header if i were to do this i'd make node* tree





for the mallocs you do .


tree=(node*) malloc (sizof (struct node ) );


optionally


tree=(node*) malloc (sizeof (node * ) ) i'm told works as well.

I'm not sure if that last line works.. (node *) is a pointer type, and he doesn't want to allocate sizeof (a pointer), he wants to allocate sizeof(node structure).
 
Originally posted by: arcain
Originally posted by: hans007
your code is pretty wrong man.



your header if i were to do this i'd make node* tree





for the mallocs you do .


tree=(node*) malloc (sizof (struct node ) );


optionally


tree=(node*) malloc (sizeof (node * ) ) i'm told works as well.

I'm not sure if that last line works.. (node *) is a pointer type, and he doesn't want to allocate sizeof (a pointer), he wants to allocate sizeof(node structure).



ididnt think it worked either, i always do it the struct way.


but i guess, node is a pointer type, but if you put a star next to it , its dereferenced to the non pointer type that its based on, or a struct node type.
 
Back
Top