This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
int foo(char *p)
{
static int i = 0;
if (*p == '\0') return i;
i++;
return foo(p+1);
}


如果将此函数放在通用库中会出现什么问题。如何修改代码以避开该问题?
如果将此代码放在通用库中,它将返回正确的字符串长度吗?

最佳答案

一个问题是状态在两次调用之间仍然存在:

foo("hello");
foo("world");


第二个调用将返回错误的结果,因为未重置i

关于c++ - 如果将其放在通用库中,此函数将导致什么错误? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15112477/

10-12 18:52