比较字符串

发布于 2024-10-19 17:22:41 字数 523 浏览 5 评论 0原文

谢谢!现在工作完美。 Java 让我变得愚蠢:(

我在比较 C 中的字符串时遇到了一些困难。当我不使用 isMorse 函数时,我得到正确的输出,但是当我使用它时,输出变得不准确并显示随机字符。据我所知告诉我,当调用 strcmp 时,变量“morse”实际上发生了变化,我认为这与“morse”不是常量有关,但我不确定如何补救,

谢谢!

char *EnglishToMorse(char english)
{
   static char *morse;

   int i;
   for (i = 0; i < LOOKUP_SIZE; i++)
   {
      if (lookup[i].character == english)
      {
         morse = lookup[i].morse;
         return morse;
      }
   }

   morse = &english;  // Problem was here!!!
   return morse;
}

Thanks! Works perfectly now. Java has made me stupid :(

I am having some difficulty comparing strings in C. I get correct output when I don't use my isMorse function, but when I use it the output becomes inaccurate and displays random characters. As far as I can tell, the variable "morse" is actually changed when strcmp is called on it. I am thinking that it has to do with "morse" not being a constant, but I am unsure of how to remedy it.

Thanks!!

char *EnglishToMorse(char english)
{
   static char *morse;

   int i;
   for (i = 0; i < LOOKUP_SIZE; i++)
   {
      if (lookup[i].character == english)
      {
         morse = lookup[i].morse;
         return morse;
      }
   }

   morse = &english;  // Problem was here!!!
   return morse;
}

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

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

发布评论

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

评论(5

牛↙奶布丁 2024-10-26 17:22:41

我有一点猜测。函数EnglishToMorse()可能会从堆栈返回一个指向内存的指针。如果是这样,在 EnglishToMorse() 之后运行另一个函数将改变该内存。这可能是由于 EnglishToMorse() 中的错误所致——声明了 char 的本地数组并返回了指向它的指针。

如果没有看到EnglishToMorse()的代码,这只是在黑暗中刺伤。您可以向我们提供更多代码供我们查看并获胜。

I have a little guess. The function EnglishToMorse() might be returning a pointer to memory from the stack. If so, running another function after EnglishToMorse() will alter that memory. This would be due to a mistake in EnglishToMorse() -- declaring a local array of char and returning a pointer to it.

Without seeing the code for EnglishToMorse(), this is just a stab in the dark. You could provide us more code to look at, and win.

鸠书 2024-10-26 17:22:41

您在 EnglishToMorse 中有一个静态变量,但它是错误的。莫尔斯电码不需要是静态的——您只需将其返回即可。但你确实需要 english 是静态的——而不是在堆栈上——因为你返回了它的地址。此外,它必须是一个以 NUL 结尾的字符串。执行类似

char *EnglishToMorse(char english)
{
   static char save_english[2]; /* initialized to 0's */ 

   int i;
   for (i = 0; i < LOOKUP_SIZE; i++)
      if (lookup[i].character == english)
         return lookup[i].morse;

   save_english[0] = english;
   return save_english;
}

“注意”的操作,但是,EnglishToMorse 的调用者必须使用结果或在再次调用 EnglishToMorse 之前保存它,因为第二次调用可能会覆盖 static_english。

You have a static variable in EnglishToMorse, but it's the wrong one. There's no need for morse to be static -- you simply return it. But you do need english to be static -- rather than on the stack -- since you return its address. Also, it needs to be a NUL-terminated string. Do something like

char *EnglishToMorse(char english)
{
   static char save_english[2]; /* initialized to 0's */ 

   int i;
   for (i = 0; i < LOOKUP_SIZE; i++)
      if (lookup[i].character == english)
         return lookup[i].morse;

   save_english[0] = english;
   return save_english;
}

Note, however, that the caller of EnglishToMorse must use the result or save it before EnglishToMorse is called again, since the second call may overwrite static_english.

倾城°AllureLove 2024-10-26 17:22:41

morse 变量出现变化的原因是它指向堆栈上的一个区域。它指向堆栈上的一个区域的原因是因为您为其分配了参数english的地址,该地址在您调用函数时被压入堆栈,然后在函数完成后从堆栈中弹出。

现在,您的莫尔斯变量将指向堆栈上相同位置的任何内存,该位置将在程序的整个生命周期中不断变化。

在我看来,解决此问题的最佳方法是如果字符不是 AZ,则从 EnglishToMorse 返回 NULL 指针...然后检查 isMorse 中的 NULL 指针代码>函数。毕竟,检查代码中的 NULL 指针是一个很好的做法。

char* EnglishToMorse(char english)
{
    int i;

    english = toupper(english);
    for (i = 0; i < LOOKUP_SIZE; i++)
    {
        if (lookup[i].character == english)
            return lookup[i].morse;
    }

    return NULL;
}

int isMorse(char* morse)
{
    int i;

    /* Check for NULL, so strcmp doesn't fail. */
    if (morse == NULL) return 0;

    for (i = 0; i < LOOKUP_SIZE; i++)
    {
        if(strcmp(morse, lookup[i].morse) == 0) 
            return 1;
    }

    return 0;
}

The reason your morse variable appears to change is because it points to an area on the stack. The reason it points to an area on the stack is because you assigned it the address of your parameter english, which got pushed onto the stack when you called your function then popped off the stack once the function completed.

Now your morse variable will point to whatever memory takes that same location on the stack, which will constantly change throughout the lifetime of your program.

In my opinion, the best way to fix this problem would be to return a NULL pointer from EnglishToMorse if the character is not A-Z... then check for the NULL pointer in your isMorse function. After all, it's good practice to check for NULL pointers in code.

char* EnglishToMorse(char english)
{
    int i;

    english = toupper(english);
    for (i = 0; i < LOOKUP_SIZE; i++)
    {
        if (lookup[i].character == english)
            return lookup[i].morse;
    }

    return NULL;
}

int isMorse(char* morse)
{
    int i;

    /* Check for NULL, so strcmp doesn't fail. */
    if (morse == NULL) return 0;

    for (i = 0; i < LOOKUP_SIZE; i++)
    {
        if(strcmp(morse, lookup[i].morse) == 0) 
            return 1;
    }

    return 0;
}
因为看清所以看轻 2024-10-26 17:22:41

看起来问题可能出在这个函数中:

char *EnglishToMorse(char english) {
    static char *morse;
    // ...
    morse = &english;
    return morse;
}

您正在返回传递到函数中的参数 (english) 的地址。该参数在函数返回后(并且在调用者有机会实际看到该值之前)不再存在。看起来好像您试图通过将 morse 变量声明为静态来解决此问题,但这只会使 morse 变量本身静态,而不是无论它指向什么。

此外,C 中的字符串必须以 NUL 字符终止。通过返回指向单个字符(如english)的指针,无法保证内存中的下一个字节是或不是NUL字符。因此,期望看到以 NUL 结尾的字符串的调用者可能会得到比他们预想的更多的结果。

It looks like the problem is likely in this function:

char *EnglishToMorse(char english) {
    static char *morse;
    // ...
    morse = &english;
    return morse;
}

You are returning the address of a parameter (english) that's passed into the function. This parameter ceases to exist after the function returns (and before the caller gets a chance to actually see the value). It appears as though you've attempted to fix this by declaring the morse variable static, but this only makes the morse variable itself static, not whatever it points to.

Also, strings in C must be terminated with a NUL character. By returning a pointer to a single character (as in english), there is no guarantee that the next byte in memory is or is not a NUL character. So the caller, who is expecting to see a NUL-terminated string, may get more than they bargained for.

踏月而来 2024-10-26 17:22:41

char *EnglishToMorse(char english)

莫尔斯电码 = &english;
是问题所在。

永远不应该返回指向局部变量或函数参数的指针。

char *EnglishToMorse(char english)
and
morse = &english;
are the problem.

You should never return a pointer to a local variable or a function parameter.

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