• 9
• 10
• 10
• 9
• 10

String vs char arrays

This topic is 3440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Concerning c++. Which one should I use? Which one is better? For example in the case I would want to store a character's name or check user input should I store them in a char array or a string(Right now those are the only two things I am concerned about with char arrays and strings but others might come up)? Is one better for certain things over the other and vice versa? Could someone point the best example? Thanks

Share on other sites
What platform? If its not a limited one (like gba, or something like taht), stick with strings. Easier to handle and less of a hassle. Then again, if you are using a lot of libraries to use your strings (and they require char arrays), consider using char*.

Share on other sites
The rule of thumb is to use std::string whenever possible. It's part of "good" C++. Checking user input (or any other form of text) is going to be a lot easier if it's a std::string rather than a char array. Example:
std::string name = getSomeName();if (name == "Bob"){    doSomething();}char* name = getSomeName();if (strcmp(name, "Bob") == 0){    doSomething();}

You tell me which of the two is cleaner (if you say the second one you're crazy [smile]).

Share on other sites
Windows xp would be the platform I think. So in this case I should stick to strings? I think I know what you mean, I've been working with directX and it always uses char*. That's something I've wondered though, why do people use char* instead of char[] I mean I sort of know how it works but why is the former method prefered?

Share on other sites
Quote:
 Original post by skullfireWhat platform? If its not a limited one (like gba, or something like taht), stick with strings. Easier to handle and less of a hassle. Then again, if you are using a lot of libraries to use your strings (and they require char arrays), consider using char*.

Have you been introduced to std::string::c_str()?

@ Antonym

Use std::string unless you can give an excellent reason not to. The alternative is lots of manual memory management, or fixed size strings.

Quote:
 why do people use char* instead of char[] I mean I sort of know how it works but why is the former method prefered?

The two are quite different. char [] is used in a local function or type to create a fixed size array.

char * is used to point to a dynamically sized array, an array that must outlive its function or when passing any array to a function. You cannot actually pass an array to a function by value, you can only pass by reference or decay to pointer.

Quote:
 I've been working with directX and it always uses char*.

You will find that many (maybe even the vast majority of) APIs use raw char arrays because it places the fewest demands on calling code. In particular, C APIs don't have a choice.

Share on other sites
Thanks for the info guys, helpful, : D.

"Have you been introduced to std::string::c_str()?"

No.. :o

Share on other sites
Quote:
 Original post by AntonymWindows xp would be the platform I think. So in this case I should stick to strings? I think I know what you mean, I've been working with directX and it always uses char*. That's something I've wondered though, why do people use char* instead of char[] I mean I sort of know how it works but why is the former method prefered?

Yes, but if you use std::string, you can retrieve a const char* from the string if you ever need by dong string.c_str(). An example:

std::string name = "Demon";
const char* str = name.c_str();

This means that you can easily convert whatever std::string you have to a const char* if you ever need to. Many libraries use const char* because of C code bases, and since C doesn't have std::string they can't use it.

Quote:
 Original post by AntonymNo.. :o

Here is a tutorial on std::string and how to use its awesomeness.
Here is a good reference for the std::string class that will tell you what each member function does and how to use it.

Share on other sites
Quote:
Original post by rip-off
Quote:
 Original post by skullfireWhat platform? If its not a limited one (like gba, or something like taht), stick with strings. Easier to handle and less of a hassle. Then again, if you are using a lot of libraries to use your strings (and they require char arrays), consider using char*.

Have you been introduced to std::string::c_str()?

@ Antonym

Use std::string unless you can give an excellent reason not to. The alternative is lots of manual memory management, or fixed size strings.

Quote:
 why do people use char* instead of char[] I mean I sort of know how it works but why is the former method prefered?

The two are quite different. char [] is used in a local function or type to create a fixed size array.

char * is used to point to a dynamically sized array, an array that must outlive its function or when passing any array to a function. You cannot actually pass an array to a function by value, you can only pass by reference or decay to pointer.

Quote:
 I've been working with directX and it always uses char*.

You will find that many (maybe even the vast majority of) APIs use raw char arrays because it places the fewest demands on calling code. In particular, C APIs don't have a choice.

Of course... i meant that if you are using full char array libraries, it might be more efficient just using plain char arrays instead of calling c_str function all the time. :)

Share on other sites
Quote:
 ... it might be more efficient just using plain char arrays instead of calling c_str function all the time.

You'd be surprised. C strings are extremely inefficient for operations that require the length to be known. std::string::length() {or size()} is a O(1) operation while strlen() is O(n).

If you are smart enough to avoid excessive copies of std::string instances then I wouldn't expect std::string to be particularly slower.

There is also programmer efficiency to consider. std::string is orders of magnitude easier to work with than trying to manually control the lifetime of raw char arrays. Code manipulating a c string is far more likely to contain bugs than the equivalent code that uses std::string.

Finally, whether code is "efficient" is something we decide after the fact. If the code runs acceptably, why put a lot of effort into improving the efficiency if you might not be able to measure the result.

Far better to wait until the code runs slowly. You can then run the code in a profiler and determine the actual bottlenecks - rather than guess and hope in advance.

Share on other sites
Quote:
 Original post by AntonymWhich one should I use? Which one is better?

std::string

Quote:
 Original post by AntonymFor example in the case I would want to store a character's name or check user input should I store them in a char array or a string

You can't use a char array to store user input because you don't know how much the user will input. For example:
char input[1000]; // lets hope the user doesn't input more than 1000 charactersstd::cin >> input; // if he does, we have a buffer overflow and thus a security hole

Code like this is responsible for 99% of the security holes in todays systems.

My advice is to NEVER use char* for data that varies at runtime, except when you have to interoperate with C APIs.