用 int 值填充 char 数组

Fill a char array with int values

本文关键字:char 数组 填充 int      更新时间:2023-10-16

我正在为Arduino项目使用一个库,该项目有一个我需要调用的函数。此函数仅接受const char*类型的一个参数。我们叫它吧。

我需要将一些int值传递给foo,所以我首先使用sprintf转换它们。目前为止,一切都好。

当我尝试用转换为char的值填充数组int然后使用数组中的每个值调用foo时,问题就来了。

我希望这能更好地解释这个问题:

#include <iostream>
using namespace std;

// This function cannot be modified because
// is a part of a library
void foo(const char *bar){
cout << "Result: " << bar << endl;
}
int main() {
char *values[10]; // My array of values
char tmp[10]; // Temporary buffer for the int > char conversion
for(int i = 0; i < 10; i++){
int samplevalue = i * 2; // Just a sample value, not important
sprintf(tmp, "%d", samplevalue); // Copy the sample value to the temporary buffer
values[i] = tmp; // Assign the value of the temp var to a position in my values array
cout << values[i] << endl;
}
cout << "==============" << endl;

// Here comes the problem:
for(int i = 0; i < 10; i++){
foo(values[i]);
}
return 0;
}

该代码的输出如下所示:

0
2
4
6
8
10
12
14
16
18
==============
Result: 18
Result: 18
Result: 18
Result: 18
Result: 18
Result: 18
Result: 18
Result: 18
Result: 18
Result: 18

如您所见,所有结果行都等于分配给 vartmp的最后一个值。我想这是因为values[10]数组中的每个值都包含一个指向 tmp 变量的指针,而不是它的实际值。

我希望在每个结果行上都有一个不同的数字,就像在第一个for循环中一样。

我想很明显,我甚至还没有接近成为C++专家,任何帮助都将不胜感激。

谢谢!

char *指针和数组不是字符串。请改用std::string

#include <iostream>
using namespace std;

// This function cannot be modified because
// is a part of a library
void foo(const char *bar)
{
cout << "Result: " << bar << endl;
}
int main(void)
{
std::string values[10]; // My array of values
char tmp[10]; // Temporary buffer for the int > char conversion
for (int i = 0; i < 10; i++) {
int samplevalue = i * 2; // Just a sample value, not important
sprintf(tmp, "%d", samplevalue); // Copy the sample value to the temporary buffer
values[i] = tmp; // Assign the value of the temp var to a position in my values array
cout << values[i] << endl;
}
cout << "==============" << endl;

// Here comes the problem:
for (int i = 0; i < 10; i++) {
foo(values[i].c_str());
}
return 0;
}

使用数组时,values数组中的所有指针都指向tmp,您可以通过循环values并像这样打印地址来检查这一点

fprintf(stdout, "%pn", values[i]);

因此,由于您sprintf()tmp所有值,因此将要打印的值始终是最后一个值,因此没有隐含的副本

values[i] = tmp;

这只是values[i]指向tmp,所以当你访问values[i]时,你真的可以访问tmp

随着std::string复制发生。

此外,您可能应该使用字符串流直接将数字写入每个values[i],因为这sprintf()非常危险。

或者最好使用像这样的真正的 c++ 解决方案,

#include <iostream>
#include <vector>
#include <sstream>
// This function cannot be modified because
// is a part of a library
void foo(const char *bar)
{
std::cout << "Result: " << bar << std::endl;
}
int main(void)
{
std::vector<std::string> values;
for (int i = 0; i < 10; i++) {
values.push_back(std::to_string(2 * i));
std::cout << values[i] << std::endl;
}
std::cout << "==============" << std::endl;
for (size_t i = 0; i < values.size(); i++) {
foo(values[i].c_str());
}
return 0;
}

请注意,现在,您可以更改values中的元素数量,如果需要,可以将其用作数组,只需阅读std::vector的文档即可。

好的,我终于让它工作了。在Arduino中,字符串被声明为String variable;c_str()函数将字符串转换为const char *,所以我将int数转换为String,然后转换为const char *

for(int i = 0; i < 10; i++){
String tmp = String(i * 2);
values[i] = tmp.c_str();
}

就是这样!它现在可以工作:)