-1

I know a technical definition (char* is a pointer to a char, which behaves similarly to an array, and a string (literal) is an array of char as it is), but how do they actually work? I read through the section on it (most of the tutorial really) on cplusplus.com (but I clearly have to re-read some sections). Still, I tried some practical examples and encountered some frustrating situations.

  1. I created a simple function, void Log(std::string message) {std::cout << message << std::endl;}, and that works great until I try to use it with char* in combination with string. For example, if I have std::string a and char* b, I cannot do Log(a + b), but I can do std::string print = “”; print += a; print += b; Log(print); Why? How should I define my Log(.) function so that I can use it similarly to Log(a+b) regardless of what types they are (at least std::string and char*, but preferably also int and other types)? If that’s possible, at least.
  2. When I do char * a = “some text”;, I get the warning conversion from string literal to ‘char *’ is deprecated. Obviously, this is not good, so why is this happening and how should I avoid this?
  3. Under which circumstances should I prefer one over the other (apart from the obvious that I sometimes am forced because of some API’s, etc)? Is it a good practice, for example, to convert all my char * to string or vice-versa?

In general, my knowledge of char * and strings (and C++ in general) has… room for improvement, so feel free to give some sources for how to get more familiar and comfortable with char *, strings, and any other related types (arrays, etc?). Although, I would very much appreciate it if someone would explain to me the two particular points above, as well.

GreatName
  • 35
  • 7
  • 2
    Forget online tutorials and pick up a C++ book. [Here are some that we recommend](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Your question(s) above will be explained by its chapter(s) on strings. – Lightness Races in Orbit Sep 16 '19 at 17:41
  • 1
    `char * a = "some text"` is invalid. it should be `const char * a = "some text"`. – Jarod42 Sep 16 '19 at 17:45
  • 1
    Are you confusing string literals (`"StringInQuotes"` is a literal) and `std::string`? – SergeyA Sep 16 '19 at 17:45
  • 2
    Also, there are 3 questions in one, each of which is a duplicate of a duplicate. – SergeyA Sep 16 '19 at 17:45
  • 1
    With regard to point 1, see: https://wandbox.org/permlink/TJ279e3NlsUve9yT – Paul Sanders Sep 16 '19 at 17:57
  • 1
    You are probably better off searching for help on [cppreference.com](https://en.cppreference.com/w/cpp/string/basic_string) instead of cplusplus.com. – Ted Lyngmo Sep 16 '19 at 17:58

1 Answers1

1

For example, if I have std::string a and char* b, I cannot do Log(a + b)

No you can. Here is a demonstrative program

#include <iostream>
#include <string>

void Log(std::string message) {std::cout << message << std::endl;}

int main() 
{
    std::string a( "Hello " );
    const char *b = "GreatName";

    Log( a + b );

    return 0;
}

Though it is better to declare the function like

void Log( const std::string &message );

For integral types you should use the family of the functions std::to_string.

#include <iostream>
#include <string>

void Log( const std::string &message ) { std::cout << message << std::endl; }

int main() 
{
    std::string a( "Hello " );
    int b = 10;

    Log( a + std::to_string( b ) );

    return 0;
}

When I do char * a = “some text”;, I get the warning conversion from string literal to ‘char *’ is deprecated.

String literals in C++ have types of constant character arrays. So you have to write

const char * a = “some text”;

Under which circumstances should I prefer one over the other

In general especially when an argument is build from several objects you should always prefer to declare the corresponding parameter as having the type std::string.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335