Print function: sending a function.. through a function?

This is a discussion on Print function: sending a function.. through a function? within the C Programming forums, part of the General Programming Boards category; I dont really know what im supposed to do here.. I have to pass this print function to another binary ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    Print function: sending a function.. through a function?

    I dont really know what im supposed to do here..
    I have to pass this print function to another binary tree print function.

    I cant change the stuff in red..
    Code:
    void print_name(char *key, void *value){
       printf("%s\n", key);
    }
    
    void *create_tree(){
        BNODE *new_tree = (BNODE *)malloc(sizeof(BNODE));
        new_tree->left = NULL;
        new_tree->right = NULL;
    
    }
    
    void print_tree(void *tree, void(*print_fct)(char *key, void *value)){
        BNODE *current = (BNODE *)tree;
        if(current == NULL){
            return;
        }else{
            print_tree(current->left, print_fct(key, value));
            printf("%s\n", current->name);
            print_tree(current->right, print_fct(key, value));
        }
    
    }
    in main:
    Code:
    void *tree = create_tree();
    ...
    print_tree(tree, print_name);

    I understand I have to send in this print function.. but is this the right way? Im getting an error saying that key and value are undeclared.. but arent they declared in the "print function" in the 2nd function field?

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Of course they are. You have to do this:
    Code:
    void print_tree(void *tree, void(*print_fct)(char *, void *), char* key, void* value)
    {
    ...
    }
    Now, since you CANNOT change print_tree as above, you have to store somewhere else teh key and value, since you won't be able to pass them as parameters.

    The only place to store them is in the tree pointer OR use global variables (with a smart no-buggish way)

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > print_tree(current->left, print_fct(key, value));
    A valid recursive call would be
    print_tree(current->left, print_fct);

    A call to the passed function pointer could be
    print_fct( current->name, NULL );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    void print_tree(void *tree, void(*print_fct)(char *key, void *value)){
        BNODE *current = tree;
    
        if(current == NULL || print_fct == NULL){
            return;
        }else{
            print_tree(current->left, print_fct);
    
            /* I assume you really wanted this: */
            print_fct(current->key, current->value);
    
            print_tree(current->right, print_fct);
        }
    
    }

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. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21