Convert a string to char in c ++

2

I have this structure and I need that in the name field I can enter a data, but I have that data in a string.

typedef struct cliente{
    int cedula;
    int numCuenta;
    char nombre[100];
    cliente *izq, *der;
}cliente;
    
asked by Johnny Pachecp 27.02.2017 в 16:55
source

1 answer

4

std::string has a method called c_str() that returns a pointer of type const char* to its internal memory.

Copying the string to char* is something that can be done with the strcpy function:

std::string cadena = "hola mundo";
char buffer[100];
strcpy(buffer,cadena.c_str());
std::cout << buffer;

By the way, C ++ is not C . The syntax that people in C ++ will expect is this:

struct cliente{
    int cedula;
    int numCuenta;
    char nombre[100];
    cliente *izq, *der;
};

Note that I have removed typedef . This design works in C ++ exactly the same as the code you have put in your example.

    
answered by 27.02.2017 / 16:57
source