0
char *_strreverse(const char *str1){
        if (str1 == NULL){
                return NULL;
        }
        char str[strlen(str1)];
        strcpy(str,str1);
        for (int i = 0; i < strlen(str) - 1; ++i){
                int t = str[strlen(str) - 1];
                memmove(str + strlen(str) - 1,str + strlen(str), 2);
                memmove(str + i,str - 1 + i, strlen(str) - i + 1);
                str[i] = t;
        }
        return str;
}

Here is the function(it should reverse string) I have warning: function returns address of local variable [-Wreturn-local-addr]

Signature should be same, static char isn't allow, malloc can't be used. What should i do?

  • 1
    You're returning a pointer to a local variable that doesn't exist after the function ends. – Retired Ninja Nov 22 '22 at 15:27
  • Yes. And what should i do – игорь кочнов Nov 22 '22 at 15:28
  • 1
    Use caller allocation. Instead of return, pass a buffer as parameter. This is addressed by any half-decent beginner level text book. – Lundin Nov 22 '22 at 15:28
  • _Signature should be same, static char isn't allow, malloc can't be used._ Well,, I'm stumped. You're not supposed to modify `str1` in place, you can't `malloc` new space, and "static char isn't allow[ed]" I assume means you can't have some kind of global `char` array or local static..? I'm out of ideas. Is this some kind of hacking challenge question? – yano Nov 22 '22 at 15:33
  • Is this some trick question like "we didn't say you couldn't use `calloc`"? Or `mmap`? Or `mach_vm_allocate` on macOS? – Siguza Nov 22 '22 at 15:44
  • `char str[strlen(str1)]; strcpy(str,str1);` is **bad** as `str[]` is 1 too small. – chux - Reinstate Monica Nov 22 '22 at 17:23

0 Answers0