keword rec不做任何事情,也不会导致任何分段错误 像这个例子
#include <iostream>
#include <string.h>
using namespace std;
//Function return a char pointer instead of a char
char* ReverseString(char *input);
int main()
{
string str;
cout << "Please Enter a String: ";
getline(cin,str);
cout << "The string you entered is: " << str << endl;
str = ReverseString(&str[0]);
cout << str;
}
char* ReverseString(char* input)
{
int size = strlen(input);
char *it;
char *revBuffer = new char[size+1];
it = revBuffer;
for(int i=0; i<size; i++)
{
*it = input[(size-1)-i];
it++;
}
*it = '\0';
//return a pointer to the reversed string
return revBuffer;
}
答案 0 :(得分:2)
在此定义中:
let rec x x = x + x
“递归”函数名x
被名为x
的参数隐藏。这就好像你有:
let rec x y = let x = y in x + x
本质上有一个隐藏递归的内部绑定。