使用 strcmp 进行比较

发布于 2024-07-14 16:03:58 字数 617 浏览 10 评论 0原文

使用 gcc C99 进行编译

我正在尝试使用字符串比较来比较 2 个字符串。 但是,我似乎在 strcmp 行上得到了堆栈转储。

**属性将包含这些,所以我正在寻找框架类型。

[name] [time] [type] [time]
[name] [callref] [type] [string]
[name] [port] [type] [int16]
[name] [frametype] [type] [int16]

这是正确的比较方法吗?

非常感谢您的任何建议,

void g_start_element(void *data, const char *element, const char **attribute)
{
    for(i = 0; attribute[i]; i++)
    {
    /* Only interested in the frametype */
        if(strcmp(attribute[i], "frametype") == 0)
        {
            /* do some work here */
        }

     }
}

compiling with gcc C99

I am trying to compare 2 string using string compare.
However, I seem to be getting a stack dump on the strcmp line.

**attribute will contain these, so I am looking for frametype.

[name] [time] [type] [time]
[name] [callref] [type] [string]
[name] [port] [type] [int16]
[name] [frametype] [type] [int16]

Is this correct way to compare.

Many thanks for any suggestions,

void g_start_element(void *data, const char *element, const char **attribute)
{
    for(i = 0; attribute[i]; i++)
    {
    /* Only interested in the frametype */
        if(strcmp(attribute[i], "frametype") == 0)
        {
            /* do some work here */
        }

     }
}

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

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

发布评论

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

评论(7

心头的小情儿 2024-07-21 16:03:58

您需要有一个空字符串才能终止 for 循环:

[name] [time] [type] [time]
[name] [callref] [type] [string]
[name] [port] [type] [int16]
[name] [frametype] [type] [int16]
null

如果没有这个,for 循环将不会终止,并且当您调用 strcmp 时,您将得到指向垃圾的 attribute[i]。

You'll need to have a null string in order to terminate the for loop:

[name] [time] [type] [time]
[name] [callref] [type] [string]
[name] [port] [type] [int16]
[name] [frametype] [type] [int16]
null

Without this the for loop won't terminate and you'll end up with attribute[i] pointing to garbage when you call strcmp.

你的呼吸 2024-07-21 16:03:58
  1. 属性数组是如何初始化的? 可能已插入 NULL 元素。
  2. 此外,数组元素必须以 NULL 结尾。
  3. 您可以考虑使用 strncmp() 作为 strcmp( 的更安全替代方案) )。
  1. How is the attribute array initialized? A NULL element may have slipped in.
  2. Also, the array element must end with a NULL.
  3. You may consider using strncmp() as a safer alternative to strcmp().
一腔孤↑勇 2024-07-21 16:03:58

此代码的上下文是 expat 解析 - 请参阅这篇文章。 属性数组是交替的名称和值,以单个 0 结尾。

除非您正在寻找名称或值等于您的测试字符串的任何属性(这有点不寻常),否则您的代码应该将 i 增加 2 而不是 1 - 以便它同时跳过名称和值。

您应该比较 attribute[i] 来匹配名称,或者比较 attribute[i+1] 来匹配值。

不要假设属性将按任何特定顺序排列。 目前您只查看属性1,这是值返回的第一个属性。 如果有多个属性,则可以按任意顺序返回它们。

The context of this code is expat parsing - see this post. The attributes array is alternating name and value, with a single 0 to terminate.

Unless you're looking for any attribute whose name or value is equal to your test string (which would be somewhat unusual), then your code should increment i by 2 rather than 1 - so that it steps over both a name and a value.

You should be comparing either attribute[i] to match a name, or or attribute[i+1] to match a value.

Don't assume that the attributes will be in any particular order. Currently you're only looking at attribute1, which is the value of the first attribute returned. If there are more than one attribute, they can be returned in any order.

肥爪爪 2024-07-21 16:03:58

您的输入数组(属性)是否以 NULL 结尾? 您没有将其列为存在,但代码需要它,否则它将进入随机内存,这是导致段错误的一个很好的原因。

插入要比较的索引和/或属性的打印,以查看它的终止行为是否符合您的预期。

不太确定问题的字符串比较部分...如果输入包含这些方括号等,那么您将永远找不到任何内容,因为您正在查看每个字符串的开头。 尝试 strstr() 如果是这种情况,它会找到子字符串。

Is your input array (attribute) NULL-terminated? You don't list it as being, but the code requires it otherwise it will just walk along into random memory, which is a good cause of segfaults.

Insert a print of the index and/or the attribute being compared, to see if it behaves the way you expect with respect to termination.

Not so sure about the string-comparison part of the question ... If the input contains those square brackets and so on, you won't ever find anything since you are looking at the start of each string. Try strstr() if that is the case, it finds substrings.

↘紸啶 2024-07-21 16:03:58

添加日志记录并转储所有属性和索引器值。 这将有助于确定出了什么问题。

Add logging and dump all the attributes and the indexer value along the way. This will help identify what's going wrong.

说好的呢 2024-07-21 16:03:58

除了不以 null 终止之外,为什么不使用 strncmp() 呢? :)

Beyond not being null terminated, why not use strncmp() ? :)

以为你会在 2024-07-21 16:03:58

我会添加对 NULL 的检查,这样

if(attribute[1] != NULL && strcmp("frametype", attribute[1]) == 0)

你就不会取消引用 NULL 指针。

I would add a check for NULL so you have

if(attribute[1] != NULL && strcmp("frametype", attribute[1]) == 0)

So you are not dereferencing a NULL pointer.

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