当我使用函数作为 strcmp 的参数时,为什么会得到不正确的值?

发布于 2025-01-18 11:23:56 字数 466 浏览 4 评论 0原文

char *FuncA()
{
    char str[50] = "ex";
    return str;
}

void FuncB()
{
    char *a = FuncA();
    char *b = FuncA();
    int i = strcmp(FuncA(), FuncA());  // 1
    int j = strcmp(a, b);  // 0
}

您好,我对 strcmp 现在的工作原理有点困惑。 FuncA() 只是一个返回字符串"ex" 的函数。 一旦保存 FuncA() 返回值并使用 strcmp,您将得到正确的答案,但如果您使用 FuncA() 作为马上使用strcmp参数,你会得到错误的答案。 你能解释一下这是为什么吗?

char *FuncA()
{
    char str[50] = "ex";
    return str;
}

void FuncB()
{
    char *a = FuncA();
    char *b = FuncA();
    int i = strcmp(FuncA(), FuncA());  // 1
    int j = strcmp(a, b);  // 0
}

Hello, I'm a little confused about how strcmp works now.
FuncA() is simply a function that returns the string "ex".
Once you save the FuncA() return value and use strcmp, you will get the correct answer, but if you use FuncA() as a parameter of strcmp right away, you will get the wrong answer.
Can you explain why this is?

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

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

发布评论

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

评论(1

音栖息无 2025-01-25 11:23:56

“调试是一个很好的学习来源。” - 可能是一个明智的人

注意:

  1. 在您的函数funca()中,您正在返回一个本地变量,该变量将一旦从功能范围中删除,该变量将被销毁。因此,funca()将返回无效的指针 不确定的行为
  2. 返回heap-allocated变量或带有static 链接
  3. funca()的返回值应为const char *,而不仅仅是char *
  4. 两个函数funca()funcb()应用static linkage
  5. 在您的功能funcb()中 声明变量ab均应为const char *,因为您以后在代码中不更改它们。
  6. 如果您的任何功能都进行零参数,请像键入func(void){}

最终代码:

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

static const char* FuncA(void)
{
  static const char str[50] = "ex"; // static linkage
  return str;
}

void FuncB(void)
{
  const char* a = FuncA();
  const char* b = FuncA();
  int i = strcmp(FuncA(), FuncA()); // 0 
  int j = strcmp(a, b); // 0

  printf("%d\n%d\n", i, j);
}

int main(void)
{
  FuncB();
  return EXIT_SUCCESS;
}

"Debugging is a great source of learning." -Probably a wise man

Notes:

  1. In your function FuncA() you are returning a local variable which will be destroyed once it gets out of the function scope. Hence, FuncA() will return an invalid pointer which will cause undefined behaviour
  2. Return heap-allocated variable or variable with static linkage
  3. The return value of FuncA() should be const char *, instead of just char *.
  4. Both the functions FuncA() and FuncB() should be declared with static linkage
  5. In your function FuncB() both the variable a and b should be of type const char * as you aren't changing them later in your code.
  6. If any of your functions take zero arguments, then write them like type func(void) { }

Final Code:

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

static const char* FuncA(void)
{
  static const char str[50] = "ex"; // static linkage
  return str;
}

void FuncB(void)
{
  const char* a = FuncA();
  const char* b = FuncA();
  int i = strcmp(FuncA(), FuncA()); // 0 
  int j = strcmp(a, b); // 0

  printf("%d\n%d\n", i, j);
}

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