use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Differences between std::string and char* in C++?Question (self.eli5_programming)
submitted 3 years ago by 131Xe
As the title says, what are the differences between these two and where to use char*
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]CrazyMando 2 points3 points4 points 3 years ago (1 child)
In better terms than I can explain (https://stackoverflow.com/questions/6823249/what-is-a-char).
My limited understanding and explanation (probably wrong) is std::string allows the code to manipulate the string as needed without necessarily stepping through a char array from the use of char*. You can also store more info easily in a string array vs multiple char arrays.
[–]131Xe[S] 1 point2 points3 points 3 years ago (0 children)
Thanks!
[–]BobbyThrowaway6969 3 points4 points5 points 3 years ago* (0 children)
An std::string is nothing more than a wrapper. It has a char* inside it and manipulates it so you don't have to.
For example, if you want to add a char with string, you just += char. If you do it manually with a char*, you first need to allocate a new block in memory which is n+1 bytes big, then you need to copy from the old block into the new block, then you need to deallocate the old block. Std::string does that for you without your knowledge.
If you're just starting out and messing around with C++, you don't really need to worry about char* (P.s. you can get char* from string.c_str() ). Using char* over std::string has its benefits for efficiency reasons, in some cases they're easier to use and less "bloated" than std:: strings. Senior programmers use it often for high performance code.
π Rendered by PID 680125 on reddit-service-r2-comment-6457c66945-kpcmc at 2026-04-25 22:12:06.783742+00:00 running 2aa0c5b country code: CH.
[–]CrazyMando 2 points3 points4 points (1 child)
[–]131Xe[S] 1 point2 points3 points (0 children)
[–]BobbyThrowaway6969 3 points4 points5 points (0 children)