C语言编程,atoi函数

标签 c

在 C 编程中,我真的对 atoi 函数感到困惑。
我也可以这样写:
atoi(年) = 年龄; 而不是:

年龄 = atoi(年);

第一个给出了警告,但仍然像第二个一样工作。哪一个是正确的?

最佳答案

不,您不能使用 atoi 或任何其他函数来做到这一点。如果 C 编译器允许这样做并生成从 atoi(years) 分配到 age 的代码,则编译器将损坏。

为了好玩,我把它放入一个小程序中:

#include <stdio.h>
#include <stdlib.h>
int main()
  {
  char *years = "61";
  int age;

  atoi(years) = age;

  printf("%d\n", age);
  }

并通过我的本地 C 编译器(HP C/aC++ 编译器 FWIW)卡住了它。产生了以下消息:

$ cc test.c

"test.c", line 9: error #2137: expression must be a modifiable lvalue
    atoi(years) = age;
    ^

"test.c", line 9: warning #2549-D: variable "age" is used before its value is
          set
    atoi(years) = age;
                  ^

1 error detected in the compilation of "test.c".

祝你好运。

关于C语言编程,atoi函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51328806/

相关文章:

c - 使用 C 和 UDP 实现停止并等待,什么导致 'resource temporarily unavailable'?

c - 它是向左旋转数组的有效程序吗?

编写十六进制和十进制数的简单加法的约定

c - 如何修复 PrintUIEntry 返回错误代码 2

c - 无法将数组分配给双指针

c - 'sprintf' : double precision in C

检查在 C 中成功创建的数组

c++ - 客户端代码 : Memory leak for particular functions 中的 Valgrind Hook

java - fortran/c/python 和 speed 中的长函数名称

c - 使用 stdint.h 和 ANSI printf?