Thread: const pointer parameter assigned to a local variable inside a function

  1. #16
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  2. #17
    Registered User
    Join Date
    Jul 2005
    Posts
    98
    Quote Originally Posted by Dave_Sinkula
    You can pass a non-const qualified value to a function with a const-qualified value. Going up doesn't hurt things, going down does.
    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.

    Quote Originally Posted by Dave_Sinkula
    Do you intend to (potentially) modify root? If so, don't tell the compiler you won't. And the same goes for any aliases you may create.
    I do not intend to modify root in the search function. It is just a search; nothing needs to be modified.

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Here you say you root might be modified:
    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;
    }
    It might even be written like this:
    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]Bah. It's late, I need to get some sleep.
    [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.*

  4. #19
    Registered User
    Join Date
    Jul 2005
    Posts
    98
    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:
    Code:
    struct node {
      node *parent, *left, *right;
      void *key, *data;
    };
    The function calling tree_search is not important. One can just
    gcc -c this file, and the gcc warning still shows up.

  5. #20

  6. #21
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM