gpt4 book ai didi

c - STM32 C : atoi converts part of string which is not an argument

转载 作者:太空宇宙 更新时间:2023-11-04 00:50:18 25 4
gpt4 key购买 nike

我已经从 GPS 接收器解析了一些日期和时间。并且需要将它们从字符串转换为 int:

char year[4] = "2014";
char month[2] = "01";
char day[2] = "24";
char hour[2] ="12";
char minute[2] = "58";
char second[2] = "39";

GPS_current_year = atoi(year);
GPS_current_month = atoi(month);
GPS_current_day = atoi(day);
GPS_current_hour = atoi(hour);
GPS_current_minute = atoi(minute);
GPS_current_second = atoi(second);

执行这些之后的结果是:

enter image description here

在转换小时字符串时,以某种方式转换了部分分钟字符串。与分钟和秒相同。

字符串并排放置在内存中。

如果我更改定义字符串的顺序,那么秒可能会添加到年等。

问题:

  • 什么可能导致这个错误?
  • 使用 atoi 有什么方法可以避免这个错误吗?

我知道我可以使用循环一次转换一个 char 。只是想找出为什么它不起作用。

最佳答案

除了字符串周围缺少的引号外,您的 char 数组的大小还应定义为再容纳一个 EOS 字符(字符串末尾为二进制零)。

因为内存表示将是例如“2014\0”

char year[4+1] = "2014";

关于c - STM32 C : atoi converts part of string which is not an argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21333645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com