Concatenating Strings

Hi all,

I am trying to do the following:

int main(int argc, char *argv[])
{
char const* compname = "ABCDEF" + getenv("COMPUTERNAME");

Some people have suggested that the "+" is not required and it should work but it doesnt...

Basically I want to prefix "ABCDEF" to the computer name and store it under the "compname".

Is there something I am missing?
Last edited on
You cannot concatenate char arrays (also known as "C strings") like this.
However if you use std::string (C++ strings) that may help.
http://www.cplusplus.com/reference/string/string/

1
2
3
4
5
6
7
8
9
10
11
#include <string>

int main()
{
    std::string compname("ABCDEF");

    // or you may write an assignment if you find it more familiar
    // std::string compname = "ABCDEF";

    compname += getenv("COMPUTERNAME");
}


Bottom line is, you cannot use operator+ to concatenate two C strings (char arrays).
You can however, use it to concatenate an std::string and a C string.
http://www.cplusplus.com/reference/string/string/operator+/
Last edited on
Topic archived. No new replies allowed.