Home > Back-end >  Converting string in char array in CPP
Converting string in char array in CPP

Time:11-12

I am working on a project and I am stuck at this for a while. I have to pass to the "write" function a char array.I tried to convert the "data" curenty string to char arrays using https://www.techiedelight.com/convert-std-string-char-cpp/ but i get only errors

error: request for member ‘c_str’ in ‘data’, which is of pointer type ‘const string*’ {aka ‘const std::__cxx11::basic_string<char>*’} (maybe you meant to use ‘->’ ?)
     char* c[] = const_cast<char*>(data.c_str());
void LCD::print(const std::string* data)
{
    for (int i = 0; i < strlen(data); i  )
        write(data[i]);
}

CodePudding user response:

const std::string*

It's hardly ever a good idea to pass a std::string pointer into a function. I recommend not doing such thing. How to replace it depends on your intentions. Reference to const is a typical default choice if you're passing a single string.

strlen(data)

The strlen function does not accept an argument of type const std::string*. This program is ill-formed.

If you want the length of a std::string, you can use the size member function.

write(data[i]);

Here, you're using subscript operator to access the ith std::string object within an array of std::string objects pointed by data.

The strlen usage and the description imply that there is only one string involved which contradicts this array iteration.


error: request for member ‘c_str’ in ‘data’, which is of pointer type ‘const string*’ {aka ‘const std::__cxx11::basic_string<char>*’}
(maybe you meant to use ‘->’ ?)
     char* c[] = const_cast<char*>(data.c_str());

The error message explains what you did wrong. data is a pointer. Pointers don't have member functions. You tried to call the member function c_str of the pointer which doesn't exist.

The error message also suggests the likely solution. If you indirect through the pointer (using the indirecting member access operator ->) to access the pointed string, then you will access the c_str member function of the string. std::string does have such member function.

There's another bug in the quoted code. You're trying to use the returned pointer to initialise an array of pointers c. That's not possible. There's only one pointer returned, and it doesn't make much sense to create an array of one elements in this case.


I have to pass to the "write" function a char array

I suspect that you actually mean, you have to pass a pointer to first element of a null terminated char array. It can be done like this:

std::string data = ...;
write(data.c_str());

It's so trivial, that I wouldn't recommend writing a function for this purpose.

CodePudding user response:

The problem is that strlen accepts a const char* while you're supplying a const string*. You can solve this by using the size() member function of std::string as follows:

void print(const std::string* data)
{
    for (int i = 0; i < (*data).size(); i  )//first we dereference the pointer named data then call the size() member function on pointed string object
        write((*data)[i]); //first we dereference the pointer named data then access the ith element of the pointed string object 
}
  • Related