Skip to content
Advertisement

Convert char array of C into string of C++

I need to convert char array of C into string c++ but the char array is an element of struct.

Code:

This is my Structure in C

typedef struct myStruct
{
    char name[50];
    char abc[50];
    ESL_BOOL status;
}MyStruct;

and I want to access name[50] in c++ but for that I have to convert it into string. I have tried strcpy and memcpy. I am using ESL_BOOL status; and it is working but confused with name[50].

Advertisement

Answer

The first thing you should change in the class name in your struct because that will likely cause problems because it is a keyword in C++.

Then for converting a C string into a C++ one, you can simply use the std::string constructor that accepts C-style strings.

std::string myCppString(MyStruct.name);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement