What's wrong with this C-implementation?


  • 0
    # include <stdio.h>
    # include <stdlib.h>
    # include <string.h>
    
    char* reverseString(char* s) {
     int n = sizeof(s)/sizeof(s[0]);
     char *r = (char *)malloc(sizeof(char)*(n+1));
     
     int i,j;
     for (i=n, j=0; i >= 0; i--, j++) {
         r[j] = s[i];
     }
         r[j+1] = '\0';
     return r;
    }
    

  • 0
    Y

    You can not use sizeof() to a pointer, it returns the size of a pointer which is a fix number.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.