October 22, 2024
Chicago 12, Melborne City, USA
C#

Why does my recursive strlen function return value 4 times bigger?


I’m trying to implement a strlen function in C with recursion and for some reason it’s returning a 4 times bigger value than expected.

int *strlen(char *s)
{
   if(*s == '\0')
       return 0;
   else
       return 1 + strlen(s+1);
}

Interestingly, when I change the type return of the function to "char", I get the desired result. I don’t understand why, it should return an int anyways. I copied this code from a book, it should be right.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video