将双插入char*数组元素中

Inserting double into char* array element

本文关键字:数组元素 char 插入      更新时间:2023-10-16

如何将双数据类型插入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)。