【问题标题】:How to convert integer to char array[]? [duplicate]如何将整数转换为char数组[]? [复制]
【发布时间】:2020-02-06 22:42:40
【问题描述】:

请读到最后。

我有一个函数循环遍历字符数组,直到在字符数组中找到“\0”结束字符。

我要转换整数

int number = 128;

到一个字符数组

char data[] = ""; // data[] = "128"  , contains '\0'

我找到了几种建议的方法(memcpy、itoa),但我想要一个在末尾自动添加“\0”的转换。

【问题讨论】:

  • 关于该评论:C 中 双引号 中的字符串文字末尾有 NUL 字符,因此 "128\0" 将有 2 个 NUL 字符。

标签: c


【解决方案1】:

itoa() 自动添加一个空终止符。 见:http://www.cplusplus.com/reference/cstdlib/itoa/

【讨论】:

    猜你喜欢
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2010-10-26
    • 1970-01-01
    相关资源
    最近更新 更多