简体   繁体   中英

How to assign char* variables dynamicly in C++?

I encounter a scenario to register arbitrary addresses as char* in a program.I need to pass each one as char* to the third party library for further action.

The third_party_function is describe as such in its header file:

virtual void RegisterFront(char *pszFrontAddress) = 0;

First, the program have to read from a config file with a group of addresses like:

tcp://18.19.20.22:7778; tcp://18.19.20.24:7778; tcp://18.19.20.25:7778; 

The procedure to analyze this string and break it to a vector of addresses is simple if I define it as a cstring and use a loop. But then each has to be registered in that function in order for the library to connect to another address if current failed. Currently the configuration has 3 addresses but an arbitrary number of them should be allowed.

A static way to assign char* variables would be like this:

...
#include third_party_lib;

char* addr1 = "tcp://18.19.20.22:7778".c_str();
char* addr2 = "tcp://18.19.20.24:7778".c_str();

RegisterFront(addr1);
RegisterFront(addr2);

I omitted the loop to analyze the addresses out from the config, but the essence is clear: I cannot name addr[n]'s in the program since the number of addresses is uncertain - it depends on how many would be written in that config line.

ie For the 3 addresses scenario I can assign addr1 addr2 addr3 in the code. But once it is compiled, what if the config line now contains only two addresses? What about changing to 5?

  • How to improve the above to achieve dynamically assigning an arbitrary number of char* variables and pass each one of them to the third party function RegisterFront ? - Of course you could replace it by some print function to try out, but the input type shall be the same.

Let's say your config line is this:

std::string s = "tcp://18.19.20.22:7778; tcp://18.19.20.24:7778; tcp://18.19.20.25:7778;"

s can also be a char*, it doesn't matter. At some point in your code, you've got a string representation of that config line that you've read from file or wherever.

Then to loop through that string for each URL and all your RegisterFront function is just this.

void trim(std::string& str) {
    
    while (s.size() > 0 && isspace(s[0])) {   
       s = s.substr(1);
    }

    while (s.size() > 0) && isspace(s[s.size()-1]) {
       s = s.substr(0, s.size()-1);
    }
}

void RegisterAddresses(const std::string& config_line) {

    std::istringstream ss(config_line);
    std::string address;
    while (std::getline(ss, address, ";")) {
        
        trim(address);
        char* data = address.data();

        RegisterFront(data);
    }
}

Then when you have your config_line (either defined as a char* or std::string instance), you just invoke it either way:

std::string s = "tcp://18.19.20.22:7778; tcp://18.19.20.24:7778; tcp://18.19.20.25:7778;"
RegisterAddresses(s);

This also works (a temporary std::string is passed to RegisterAddresses - constructed from the char* passed in)

const char* s = "tcp://18.19.20.22:7778; tcp://18.19.20.24:7778; tcp://18.19.20.25:7778;"
RegisterAddresses(s);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM