Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Comparing dynamically allocated strings in C

I’m working on a case where I’m dynamically populating a char array based on the return value of another function.

At some point, I would like to compare the incoming value to a static string and trigger an action.

For some reason, I couldn’t get the strcmp function to work. Please see the code below and the respective output.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char* word_generator(int selector)
{
    switch(selector)
        {
            case 0:
                return "a";
                break;
            case 1:
                return "jk";
                break;
            case 2:
                return "dfr";
                break;
            case 3:
                return "sbjk";
                break;
            default:
                printf("ERROR: Request out of range!\n");
    }
    return "";
}


int main () {
    for(int i = 0; i < 4; i++)
    {
        printf("Test string[%d]: %s\n", i, word_generator(i));
        printf("\t__STRLEN: %lu\n", strlen(word_generator(i)));

        char* input_char_buffer = malloc(strlen(word_generator(i))+1);
        strcpy(input_char_buffer, word_generator(i));
    
        printf("\tCurrent buffer (value: %s, length: %zu)\n", input_char_buffer, strlen(input_char_buffer));

        char key[] = "dfr";
        printf("\tCurrent key (value: %s, length: %zu)\n", key, strlen(key));

        /* if(strlen(input_char_buffer) == strlen(key)) */
        /* { */
        /*     printf("\t\tOK\n"); */
        /* } */

        int ret;
        ret = strcmp(input_char_buffer, key);
        printf("\t__STRCMP: %d\n", ret);

        if(ret == 1)
        {
            printf("\t\tOK\n");
        }

        // Clean-up.
        free(input_char_buffer);
        input_char_buffer = NULL;
    }

    return 0;
}

OUTPUT:

Test string[0]: a
    __STRLEN: 1
    Current buffer (value: a, length: 1)
    Current key (value: dfr, length: 3)
    __STRCMP: -3
Test string[1]: jk
    __STRLEN: 2
    Current buffer (value: jk, length: 2)
    Current key (value: dfr, length: 3)
    __STRCMP: 6
Test string[2]: dfr
    __STRLEN: 3
    Current buffer (value: dfr, length: 3)
    Current key (value: dfr, length: 3)
    __STRCMP: 0
Test string[3]: sbjk
    __STRLEN: 4
    Current buffer (value: sbjk, length: 4)
    Current key (value: dfr, length: 3)
    __STRCMP: 15

As you can see, the debug bits are the correct values, but for some reason the strcmp is returning garbage values.

>Solution :

Don’t worry, these numbers are not trash, it’s just how strcmp works.

strcmp returns 0 whenever both strings match, if they don’t, it returns the difference between the first char of both strings in the ASCII table.

For instance:

The value of d from dfr is 100 in the ASCII table, while s from sbjk is 115. Thus, 115 - 100 = 15, and that’s the return you’re getting.

It seems you just want to check if both strings are equal or not. For this, I suggest you to use !strcmp() instead of strcmp. This way, you will have 1 if strings match or 0 if not. You can check later how the logical operand ! works and why this is happening.

You can see the change working:

Test string[0]: a
    __STRLEN: 1
    Current buffer (value: a, length: 1)
    Current key (value: dfr, length: 3)
    __STRCMP: 0
Test string[1]: jk
    __STRLEN: 2
    Current buffer (value: jk, length: 2)
    Current key (value: dfr, length: 3)
    __STRCMP: 0
Test string[2]: dfr
    __STRLEN: 3
    Current buffer (value: dfr, length: 3)
    Current key (value: dfr, length: 3)
    __STRCMP: 1
        OK
Test string[3]: sbjk
    __STRLEN: 4
    Current buffer (value: sbjk, length: 4)
    Current key (value: dfr, length: 3)
    __STRCMP: 0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading