char[] vs string

Pages: 12
You have to do it yourself using the quote buttons on the right-hand side in the post menu.
e.g.
[quote=SlowCoder] ... [/quote]
Last edited on
pointers may have a bit of syntax but its a lot like a C array.
you have an index in a C array: var[index]. Index is like the pointer: its not the data, its where it is located. The weird syntax aside, its not much more than this + knowing that you have to ask for and give back chunks of memory if doing dynamic memory (new and delete). If it seems more complex than this, ask a question.
C introduced types, it's predecessor had a single type, a word.

The types were, of course, char, int, float and their variants.

A string type was added too, but not to the language, but in a library. It was added as an abstract data type; data and a set of functions. The data? A zero terminated array of char. The functions? strcpy, strcat, ...

With the data exposed, and all responsibility placed on the user, there were inevitable reliability problems; same as COM I guess.

C++ used the information hiding features of the language to create a safer to use string, but still pretty basic. std::string isn't really a usable string, just a replacement for C strings.
Last edited on
A string is a collection of character arrays. Strings are more easy to read for the compiler as compared to chars.
No - std::string is not a collection of character arrays.

My compiler has grown up and now can read long words.

I love sprintf.


I love snprintf() even better!

The snprintf function truncates the output when len is greater than or equal to count, by placing a null-terminator at buffer[count-1]. The value returned is len, the number of characters that would have been output if count was large enough. The snprintf function returns a negative value if an encoding error occurs.


So pass len as 0 and get the number of chars required for make_unique() etc.
Topic archived. No new replies allowed.
Pages: 12