2

I have seen tutorials that are using an array of characters in order to demonstrate something with a string object. For example thees tutorials:

http://www.cplusplus.com/reference/string/string/copy/

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

I HAVE seen tutorials that are not using a char array in order to demonstrate something. At school, the teacher also doesn't use any arrays. For me, using an array is a bit confusing at first when I'm reading the tutorial (knowing that I'm still a beginner at C++).

I'm just curious to know why are there tutorials that use a char array in order to show one or more of the things that string objects can do.

AlexSavAlexandrov
  • 858
  • 2
  • 13
  • 34

4 Answers4

6

Storing strings in arrays of characters was the original way to represent a string in the C language. In C, a string is an array of type char. The size of the array is the number of characters, + 1. The +1 is because every string in C must end with a character value of 0. This the NULL terminator or just the terminator.

C-style strings are legal in C++ because C++ is intended to be backwards compatible with C. Also, many library and existing code bases depend on C-style string.

Here is a tutorial on C-style strings. http://www.cprogramming.com/tutorial/c/lesson9.html

FYI: To convert a C++ String to a C-style string, call the method c_str().

ahoffer
  • 6,347
  • 4
  • 39
  • 68
2

In C and C++ strings are/were unusually represented as '\0' terminated arrays of char. With C++ you can use the standard class string, but that is by no means a "natural" thing to do, when representing strings. Many C and C++ programs are still quite content using an array of char.

Xeo
  • 129,499
  • 52
  • 291
  • 397
Mithrandir
  • 24,869
  • 6
  • 50
  • 66
  • I'm not surprised. In fact I was suspecting that reason. – AlexSavAlexandrov Jan 20 '12 at 21:06
  • 2
    `std::string` is a much more natural way to do it, and nobody is content with null terminated strings- they simply cannot afford to re-write their programs to get rid of it. – Puppy Jan 20 '12 at 21:14
  • @DeadMG: that my be your opinion. Many applications that need to handle "strings" and are written in c/c++ do not need need the overhead of std::string. And there are still programmers, especially developers who work with micro controllers, who still use an array of char and don't complain about it. – Mithrandir Jan 20 '12 at 21:21
  • @Mithrandir Which overhead? At least since `move` most performance problems of strings are a thing of the past (and even before that it didn't matter much) and strings have several decided advantages compared to null terminated char arrays. I seriously doubt these claimed performance advantages are measurable of even existing in any real world program. – Voo Jan 20 '12 at 21:29
  • @Mithrandir: There is no significant overhead to `std::string`. And developers for micro-controllers are a fraction of developers as a whole, and "don't complain" != "wouldn't be better served using `std::string`", and indeed, failing to serve a very specific niche does not mean that it fails to serve it's general purpose. – Puppy Jan 21 '12 at 01:40
  • @Mithrandir: `written in c/c++ do not need need the overhead of std::string` -> Firstly, there is no language called C/C++, though there are two languages called C and C++. And there is no std::string in C. – Sebastian Mach Jan 14 '13 at 16:44
1

See the Bjarne Stroustrup's paper "Learning Standard C++ as a New Language" www2.research.att.com/~bs/new_learning.pdf

  • I guess this answer means that there is an algorithm explained in this book, that answers my question. Right? – AlexSavAlexandrov Jan 21 '12 at 06:42
  • It is not a book, it is a paper (just 11 pages). Bjarne is the creator of C++. In the paper, Bjarne explains how to use C++ as a new language and not as an extension of C. Programming in the C++ way. I do not know if answer your question, but can guide you on best programming practices in C++, avoiding C error prone constructions. – Fernando Pelliccioni Jan 23 '12 at 12:16
0

Strings appeared when the STL appeared, when the C++ standard was formed somewhere in the 1990s if i remember well. Until then (for example Turbo C++ which is still used at my school... unfortunately), there was no 'string' object in C++, so everyone used char arrays. They are still widely used, because strings don't really introduce many new things that char arrays can't do, and many people don't like them. Strings are in fact null ended char arrays, but they hide this behind a class.

One problem with strings is that not all library functions support them. For example, the printf family of functions, atoi (which comes from 'ascii to integer', also atof and all the others), these don't support strings. Also, in larger projects, sometimes you need to work in C, and strings don't exist in C, only char arrays.

The good thing about strings is that they are implemented in such a way that it is very easy to convert from and to char arrays.

Tibi
  • 4,015
  • 8
  • 40
  • 64
  • [Is std::string part of the STL?](http://stackoverflow.com/questions/5972546/is-stdstring-part-of-the-stl) might be an interesting read for you. I believe you mean stdlib, or standard library, which is *not* the STL. – Xeo Jan 20 '12 at 21:14
  • 1
    Strings introduce several things that `char` arrays can't- performance, and safety, and encapsulation. Oh, and did I forget being generic? And all of the `atoi` functions have C++ equivalents. – Puppy Jan 20 '12 at 21:15
  • Well, it still uses containers, so it depends on STL... close enough. It's a pretty disputed fact. – Tibi Jan 20 '12 at 21:19
  • What do you mean "it uses containers"? – fredoverflow Jan 21 '12 at 11:34
  • Take a look here: http://www.sgi.com/tech/stl/basic_string.html. String is in fact a typedef of basic_string. If you look at the column "Where defined", it has members defined in Container, and Sequence. It may even be a derived class based on Sequence, but i haven't seen the source code. – Tibi Jan 21 '12 at 20:22