Do you see what is wrong here?
Code:result = (*comp)(cur_item->key, keyy);
Do you see what is wrong here?
Code:result = (*comp)(cur_item->key, keyy);
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Are you referring to "root"? I do not quite understand what you mean by passing a non-const qualified value to a function with a const-qualified value. Are you saying I can declare root in the calling function as a non-const variable, and decalre root as a const param in the called function? That's what I am trying to do; but the compiler doesn't let me.Originally Posted by Dave_Sinkula
I do not intend to modify root in the search function. It is just a search; nothing needs to be modified.Originally Posted by Dave_Sinkula
Here you say you root might be modified:
It might even be written like this:Code:typedef int t_compare_func(const void *, const void *); struct node *tree_search(struct node *root, const void *keyy, t_compare_func *comp) { struct node *cur_item; int result; if (root == NULL) return NULL; cur_item = root; while (cur_item != NULL) { result = (*comp)(cur_item->key, keyy); if (result == 0) break; else if (result > 0) cur_item = cur_item->left; else cur_item = cur_item->right; } return cur_item; }[edit]Bah. It's late, I need to get some sleep.Code:struct node *tree_search(struct node *root, const void *keyy, t_compare_func *comp) { int result; if (root == NULL) return NULL; while (root != NULL) { result = (*comp)(&root->key, keyy); if (result == 0) break; else if (result > 0) root = root->left; else root = root->right; } return root; }
[edit=2]Why not just post the actual code and describe the issues? You'd likely get faster results that way.
Last edited by Dave_Sinkula; 11-10-2005 at 11:50 PM.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
I don't see how root might be modified in the first case. The value of teh pointer itself, root, is not changed. The stuff pointed to by root is not changed either.
root is not changed in the first case but root is changed in the second case, I think.
And that's the actual code. Here is the node, for the sake of completeness:
The function calling tree_search is not important. One can justCode:struct node { node *parent, *left, *right; void *key, *data; };
gcc -c this file, and the gcc warning still shows up.
Okay, not as tired now. [I stand corrected; and I blame my poor post on cheap American beer.]
I'd say use the cast on the return value if you don't choose to const-qualify the return value. Or else const-qualify the return value and cast the return value in the calling function. Programmer's choice.
I don't know that I'd call this undue inconvenience. I'd consider it like adding break to a switch statement's cases -- 97% of the time it's what you want to do (like not having the cast), but there is that 3% remaining (like this, where you'd need the cast somewhere to avoid the warning).
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*