C: sprintf and recursion
        Posted  
        
            by Shinka
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Shinka
        
        
        
        Published on 2010-05-11T18:40:40Z
        Indexed on 
            2010/05/11
            18:44 UTC
        
        
        Read the original article
        Hit count: 427
        
In C, is it possible to use recursion within the sprintf function ? For some reason I get a segmentation fault when I do it:
inline char *TreeNode_toString(const TreeNode *node)
{
  char *out;
  if(TreeNode_isExternal(node)) // If the node has no children...
  {
    sprintf(out, "%s:%.2f", node->name, node->distance);
  }
  else // The node is strictly binary, so it will have two non-null children
  {
    char *l = TreeNode_toString(node->l); // l = left child
    char *r = TreeNode_toString(node->r); // r = right child
    sprintf(out, "(%s,%s):%.2f", l, r, node->distance);
  }
  return out;
}
© Stack Overflow or respective owner