GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

C: Pointer problems in a function

I am trying to call function pointer to another function and I am getting the incompatible pointer type message.

The following part of code is string manipulation (comparison):

typedef struct node
{
    char ch;
    struct node *next;
}STRING;

typedef struct tree_node
{
    STRING str;
    struct tree_node *left,*right;
}NODE;

NODE *add_to_tree(NODE *root,STRING *str,int((*cmp_strings)(STRING *,STRING *)))
{
   STRING *str_temp;
   root->str=*str_temp;
   if(root == 0)
        return form_node(str);
   if(((*cmp_strings)(str,str_temp)) < 0)
        root->left=add_to_tree(root->left,str,&cmp_strings);
   else root->right=add_to_tree(root->right,str,&cmp_strings);
   return root;
}

What is wrong in function add_to_tree()?

asked Nov 9, 2015 by display_error

1 Answer

The name of Function itself acts as a pointer to function (same as name of array acts as a pointer)

that is why your program is giving pointer compatibility error.

Use           add_to_tree(root->left, str,  cmp_strings);  

instead of    add_to_tree(root->left, str,  &cmp_strings);            // change for right too

  because &cmp_string represents pointer to pointer    while cmp_string is a pointer to function

answered Nov 9, 2015 by utkarsh111
...