比较C中动态分配的字符串

发布于 2025-02-02 22:27:07 字数 2303 浏览 2 评论 0原文

我正在研究基于另一个函数的返回值动态填充字符阵列的情况。

在某个时候,我想将传入值与静态字符串进行比较并触发动作。

由于某种原因,我无法使strcmp函数工作。这是我的代码和各自的输出。

代码:

#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;
}

输出:

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

您可以看到,调试位是正确的值,但是由于某种原因,strcmp是返回垃圾值。

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. Here is my code and the respective output.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羅雙樹 2025-02-09 22:27:08

不用担心,这些数字不是垃圾,而是strcmp的工作方式。

strcmp返回0每当两个字符串匹配时,如果它们不匹配,则返回ASCII表中两个字符串的第一个char之间的区别。

例如:

d的值来自dfr100在ASCII表中,而s来自> s sbjk 是115。因此,115-100 = 15,这就是您要获得的回报。

看来您只想检查两个字符串是否相等。为此,我建议您使用!strcmp()而不是strcmp。这样,如果字符串匹配或0(如果不是),则将具有1。您可以稍后检查逻辑操作<代码>!的工作方式以及为什么会发生这种情况。

您可以看到更改工作:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文