这是一个返回反向字符串的代码,如“ABC”返回“CBA”,但它返回这个“CBA═²²²²ß♣Y*&s”。怎么了?
char* inv(char* C)
{
int lenght = strLenght(C)-1;
int idx=0;
char* tempStr = (char*)malloc(lenght+2);
for (;lenght>=0;lenght--,idx++)
{
tempStr[idx] = C[lenght];
}
return tempStr;
}
int strLenght(char* str)
{
int lenght=0;
while(str[lenght] != '\0')
lenght++;
return lenght;
}
int main(int argc, char *argv[])
{
char* st= "ABC";
char* sr = inv(st);
printf("%s",sr);
}