C: sprintf and recursion
- by Shinka
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;
}