Thread: some questions regarding this function..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    some questions regarding this function..

    Code:
    /*
     Given a binary tree, return true if a node
     with the target data is found in the tree. Recurs
     down the tree, chooses the left or right
     branch by comparing the target to each node.
    */
    static int lookup(struct node* node, int target) {
      // 1. Base case == empty tree
      // in that case, the target is not found so return false
      if (node == NULL) {
        return(false);
      }
      else {
        // 2. see if found here
        if (target == node->data) return(true);
        else {
          // 3. otherwise recur down the correct subtree
          if (target < node->data) return(lookup(node->left, target));
          else return(lookup(node->right, target));
        }
      }
    }
    i thought there is no true/false in C only 0 and not 0
    ??
    if this function return true or false how come it defined as integer type??

    what the meaning of static in the signature of this function

    i know that in JAVA we put static when we dont want to call a constructor
    and straight away get the data from the variable

    but here its a function and its not java
    ??

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    #include <stdbool.h>

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    static means "don't let code outside this compilation unit see this function (or variable)" - so it's just a way to prevent the namespace to be cluttered up and/or that someone who shouldn't be calling the function from calling the function. It also shows the reader that "no external functions will call this function directly".

    As to true and false, there's no such thing in standard C, but that's not stopping people from defining their own, e.g.
    Code:
    typedef enum { false, true } bool;
    or something similar.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    -- i thought there is no true/false in C only 0 and not 0
    Including <stdbool.h> as master5001 suggests adds "bool", "true", and "false",
    although it is simple to define it yourself as matsp points out.

    --if this function return true or false how come it defined as integer type??
    Yes, it should say it returns bool to be consistent, but bool is really just an int anyway.

    --what the meaning of static in the signature of this function
    It just means that this function cannot be called from outside this file in a multi-file project.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i read that static function is "restrict visibility of the function to the translation unit in which it's declared. "

    but its not object oriented program there is no classes here where there could be a restriction

    ??

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok so static restricts it to the current file.
    what restriction are on static variable?

    regarding the boolean thing
    when i add
    Code:
    #include <stdbool.h>
    then i can use true and false
    do i need to change the function to boolean type or bool type ??
    if i will keep the function defined as an integer
    what number will the function return if we return true

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    regarding this line:
    Code:
    typedef enum { false, true } bool;
    is there a mistake
    i cants see whats the type of true ,false variables?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by transgalactic2 View Post
    regarding this line:
    Code:
    typedef enum { false, true } bool;
    is there a mistake
    i cants see whats the type of true ,false variables?
    You should read your section on enums again. false and true are not member variables of a struct, they are tags of an enumeration.

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what number will my function return in case of true
    and false?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Unless you specify otherwise, the first tag in an enum has the value 0, the next 1, and so on.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    what number will my function return in case of true
    and false?
    As tabstop says, first value in an enum defaults to zero, and any subsequent ones are one more than the previous one. You can change that by assigning specific values to each enum value, e.g.
    Code:
    enum {
       three = 3,
       twelve = 12,
       ten = 10,
       eleven     // Note, since the previous is 10, this will be eleven. 
    };
    As to what actually happens in your code-sample, we'd need to know how the enum or whatever that defines true and false is actually defined. One would assume that true is non-zero and that false is zero, so that normal C "boolean" logic works.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM