比较字符串
谢谢!现在工作完美。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我有一点猜测。函数
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 afterEnglishToMorse()
will alter that memory. This would be due to a mistake inEnglishToMorse()
-- declaring a local array ofchar
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.您在 EnglishToMorse 中有一个静态变量,但它是错误的。莫尔斯电码不需要是静态的——您只需将其返回即可。但你确实需要 english 是静态的——而不是在堆栈上——因为你返回了它的地址。此外,它必须是一个以 NUL 结尾的字符串。执行类似
“注意”的操作,但是,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
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.
morse
变量出现变化的原因是它指向堆栈上的一个区域。它指向堆栈上的一个区域的原因是因为您为其分配了参数english
的地址,该地址在您调用函数时被压入堆栈,然后在函数完成后从堆栈中弹出。现在,您的莫尔斯变量将指向堆栈上相同位置的任何内存,该位置将在程序的整个生命周期中不断变化。
在我看来,解决此问题的最佳方法是如果字符不是 AZ,则从 EnglishToMorse 返回 NULL 指针...然后检查
isMorse
中的 NULL 指针代码>函数。毕竟,检查代码中的 NULL 指针是一个很好的做法。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 parameterenglish
, 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 yourisMorse
function. After all, it's good practice to check for NULL pointers in code.看起来问题可能出在这个函数中:
您正在返回传递到函数中的参数 (
english
) 的地址。该参数在函数返回后(并且在调用者有机会实际看到该值之前)不再存在。看起来好像您试图通过将morse
变量声明为静态来解决此问题,但这只会使morse
变量本身静态,而不是无论它指向什么。此外,C 中的字符串必须以 NUL 字符终止。通过返回指向单个字符(如
english
)的指针,无法保证内存中的下一个字节是或不是NUL字符。因此,期望看到以 NUL 结尾的字符串的调用者可能会得到比他们预想的更多的结果。It looks like the problem is likely in this function:
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 themorse
variable static, but this only makes themorse
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.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.