将双插入char*数组元素中
Inserting double into char* array element
如何将双数据类型插入char*
数组?我已经尝试使用std::to_string()
以及c_str()
,但是似乎都没有做到这一点。
本质上,我正在尝试使用execvp()
调用C 程序,从阅读MAN页面,我认为它需要在char*
数组中进行。
我正在尝试调用该程序,然后将a, b, and c
作为程序execvp()
的argv
参数传递。
例如。当a = 0.1, b =0.1, c = 0.1
execvp(args[0], args)
应与终端调用相同时:./RL 0.1 0.1 0.1
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <string>
int main()
{
char *args[5];
args[0] = "RL";
std::string temp;
for(double a = 0.1; a < 1; a += 0.1){
for(double b = 0.1; b < 1; b += 0.1){
for(double c = 0.1; c < 1; c+= 0.1){
temp = std::to_string(a); //attempted to convert the double to a string
args[1] = temp.c_str(); //attempted to convert the string to c string
temp = std::to_string(b);
args[2] = temp; //attempted to just insert the string
args[3] = c; //attempted to insert as double
std::cout << "Calling: " << a << " " << b << " " << c << std::endl;
execvp(args[0], args);
}
}
}
return 0;
}
我得到这样的错误:
RLdriver.cpp:14:32: error: assigning to 'char *' from incompatible type 'const
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >::value_type *' (aka 'const char *')
args[1] = temp.c_str();
~~~~~^~~~~~~
RLdriver.cpp:16:27: error: assigning to 'char *' from incompatible type
'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char>
>')
args[2] = temp;
^~~~
RLdriver.cpp:17:27: error: assigning to 'char *' from incompatible type 'double'
args[3] = c;
编辑:我知道exec()
在替代当前程序时不会真正起作用,但是我知道如何以后如何处理,只是试图使Args的通过。
此代码有两个问题:
-
您不能在尝试时用临时对象填充" char Pointers"
args
,这不会反映您的需求。而是使用(请参见下文)const char* const args[]
,然后将其用指针填充到真实对象数据tempa,tempb,tempc。 -
execvp期望
char* const[]
时,在C 中,您将始终产生const char* const[]
,因此您需要施放诸如execvp(const_cast<char* const*>(args));
之类的东西
您可以将代码更改为,然后将有效:
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <string>
int main()
{
for(double a = 0.1; a < 1; a += 0.1){
for(double b = 0.1; b < 1; b += 0.1){
for(double c = 0.1; c < 1; c+= 0.1){
std::string tempa = std::to_string(a);
std::string tempb = std::to_string(b);
std::string tempc = std::to_string(c);
const char* const args[] = {tempa.c_str(), tempb.c_str(), tempc.c_str(), nullptr};
std::cout << "Calling: " << a << " " << b << " " << c << std::endl;
execvp(args[0], const_cast<char*const*>(args));
}
}
}
return 0;
}
(测试)
使用
args[1] = temp.c_str();
是一个问题,因为LHS为char*
型,RHS为char const*
型。您不能将char const*
分配给char*
。
一个简单的修复是使用:
args[1] = strdup(temp.c_str());
strdup
不是标准库功能,但有些编译器本地支持它。如果您的编译器不支持它,那么自己实施它并不难。在网上查找,您很可能会找到一个。
确保您对strdup
返回的字符串进行处理。您将必须咨询编译器的文档,以弄清楚如何在strdup
中分配内存。您需要使用适当的交易机制。
您的第一种方式,即
temp = std::to_string(a); //attempted to convert the double to a string
args[1] = temp.c_str(); //attempted to convert the string to c string
是正确的,除了您需要将声明char *args[5];
更改为const char *args[5];
。当然,如果要通过3个不同的命令行参数,则需要3个不同的temp
变量(temp1, temp2, temp3
)。
- 数组元素打印的递归方法
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 为什么使用数组元素查找最大数字的程序不起作用?
- 为什么将指针重新分配给 1D 数组的 char 时显示错误,但 2D 数组工作正常?
- 数组元素更改值?
- 如何访问宏中定义的数组元素
- 将数组元素递增 1
- C++函数,它将数组、谓词和运算符作为参数,并将运算符应用于满足谓词的数组元素
- 存储指向动态数组元素的指针
- CPPYY/CTYPES 将字符串数组作为 char* args[] 传递
- 为什么在 c++ 中分配 char 数组元素时,分配的字符会被销毁?
- 将双插入char*数组元素中
- 将整数值分配给 char 数组元素
- 如何在 c++ 中将 char 数组元素传递给函数
- 如何连接char数组元素并将其强制转换为float
- 将两个单词读入一个char数组元素
- 获取数组中char元素的索引
- 从char*访问数组元素
- 用const char初始化2D char数组元素
- 连接一个char 2D数组元素和c++中的int