Function is returning null instead of array in C

109 views Asked by At

So I am solving the "Longest Common Prefix" problem on Leetcode in C and have almost got it working. My Code:

    char * longestCommonPrefix(char ** strs, int strsSize){
    int minlen = strlen(strs[0]);
    for(int i=0; i<strsSize; i++){
        int len = strlen(strs[i]);
        if(len<minlen){
            minlen=len;
        }
    }

    int len=0;
    for(int i=0; i<minlen; i++){
        int match=1;
        char check = strs[0][i];
        for(int j=0; j<strsSize; j++){
            if(strs[j][i]!=check){
                match=0;
                break;
            }
        }
        if(match==0){
            break;
        }
        else{
            len++;
        }
    }

    if(len==0){
        return "";
    }
    
    char ret[len];
    for(int i=0; i<len; i++){
        ret[i] = strs[0][i];
    }
    for(int i=0; i<len; i++){
        printf("%c",ret[i]);
    }
    return ret;
}

Printing out the array ret is working out and is giving expected output. But when I am returning the char array ret, it is returning (null). So the array is storing the value correctly, but there is some issue with returning it. Can someone please help with this?

1

There are 1 answers

4
Vlad from Moscow On

If the function returns only a pointer

char * longestCommonPrefix(char ** strs, int strsSize){

then it should return a pointer to a string. The local variable length array ret declared like

char ret[len];

is not built to store a string.

Moreover it will not be alive after exiting the function because it has automatic storage duration. As a result the returned pointer will be invalid and dereferencing it will invoke undefined behavior.

On the other hand, in this return statement

if(len==0){
    return "";
}

you are returning a string (string literal) that has static storage duration.

You need to allocate memory dynamically for the resulted string in the both cases.

Also this code snippet

int minlen = strlen(strs[0]);
for(int i=0; i<strsSize; i++){
    int len = strlen(strs[i]);
    if(len<minlen){
        minlen=len;
    }
}

is just redundant. Pay attention to that you should use the unsigned integer type size_t instead of the signed type int for string lengths.

And to copy one character array in another character array it is better to use standard C string function memcpy instead of using a manually written loop.

The function can look for example the following way

char * longestCommonPrefix( char **s, size_t n )
{
    char *result = NULL;

    if (n != 0)
    {
        size_t len = 0;

        if (n == 1)
        {
            len = strlen( s[0] );
        }
        else
        {
            for (int equal = 1; equal && s[0][len] != '\0'; len += equal)
            {
                size_t i = 1;

                while (i < n && s[i][len] == s[0][len]) ++i;

                equal = i == n;
            }
        }

        result = malloc( len + 1 );

        if (result != NULL)
        {
            memcpy( result, s[0], len );
            result[len] = '\0';
        }
    }

    return result;
}