C-String and String

Write your question here.

I know a lot of people talk about the difference between C-String and String.

C-String is a sequence of chars stored in an array followed by a vector, and String is an object where a sequence of chars are stored in a vector.

First off is that correct?

Secondly i'm trying to figure out how to distinguish when one is used. The solution i'm coming up with is unless you see an array declaration then it's a c-string. If you see an String object declaration its a String object. If you see a declaration without an identifier it's a C-String? I suppose what I mean is when you have just a basic sequence of letters without a definition i.e like the following code is it by default created as a c-string?

  Bigint b;
b = "99999999999";

A c-string is an array of characters as you said (null terminated of course):

A String is a class provided by the c++ standard library. Internally it stores the string as a c-string, but it provides the user with many useful member functions, and takes care of most of the grimy pointer stuff for you.

std::string s = "My string";
s += " appended";
int string_size = s.size();

You'll usually want to use the std::string class unless your code needs to be c-compatible.

If I understand correctly, then no I don't think that C-String or string ends up being stored in a vector containers. They are 3 different things.

char *c = "Hello";
char c[] = "Hello";


string str = "Hello";

C-strings is an object of type char array or char pointer (which are pretty much the same thing I think) and strings are objects of type string. Keyword here being type. Whichever type the object is is what it is. Beyond that are their functionality, which string trumps over c-string most of the time.

If it's just


Then it's known as a string literal. Not sure what your example is on about though.
The important thing to remember is that a C-string is just an array of characters, of a "string" of characters if you will. And because it is an array, the variable is basically a pointer to a row of characters ending with the null-terminating character (\0). C-string variables are different from other pointers in that dereferencing them will return the entire C-string and not just the first character.

There are three interpretations of the C++ string. The first, because std::string is used so often like a regular primitive type, is that std::string is just another variable type.
If you want to get technical, std::string is really a class in the std namespace as BigBlackSheep points out.
Since C++11 (I think), std::string also became a container like std::vector or std::array, gaining the same capabilities such as a nested iterator class. Internally, std::string probably copies some of the procedures used by other STL containers and makes use of the cow_ptr smart pointer from <memory>.*

*Read this in some smart pointer tutorial that I cannot find anymore.
The example was regarding function overloading and the use of constructors in type conversion.

I suppose what confused me is:

Say if the constructor for the Bigint had a C-String constructor. My example is passing a string literal as Olysold points out. Is a string literal stored as a string literal or is it instantly type converted?

If it is instantly type converted, is it converted to a c string or a string?
BigBlackSheep wrote:
A String is a class provided by the c++ standard library. Internally it stores the string as a c-string,

Not exactly.

Consider this:
    char cstr[6]     = "apple";
    std::string sstr = "apple";
    cout << "cstr: " << cstr << " sstr: " << sstr << endl;
    cstr[2] = 0;
    sstr[2] = 0;
    cout << "cstr: " << cstr << " sstr: " << sstr << endl;

cstr: apple sstr: apple
cstr: ap sstr: ap le

Notice that the 0 character terminates the c-string, but not the std::string.
C string (unlike the C++ string) is not a type, it's a concept. A contiguous sequence of chars up to and including the first zero is a "C string", regardless how or where it is stored - in an array, in a vector, in a C++ string, or in a string literal

the following code is it by default created as a c-string?

Bigint b;
b = "99999999999";

"99999999999" is a string literal, which is a static array of 12 const char (11 chars with value '9' and one char with value '\0'). The contents of this array happen to be a valid C string.

If it is instantly type converted, is it converted to a c string or a string?

There is no "c string" type, so it can't be "converted" to that. The type of "99999999999" is const char[12].

If your Bigint has a constructor that accepts const char (&)[12], that constructor will be called and will access the array by reference.

If your Bigint has a constructor that accepts const char*, array-to-pointer conversion will be performed and a temporary pointer to the first character of the array will be constructed and passed to the constructor of Bigint.

If your Bigint has a constructor that accepts std::string or const std::string&, the converting constructor of std::string that takes const char* will be called and a temporary C++ string will be constructed holding a copy of your literal and that will be passed to the constructor.
Thanks Cubbi. That all makes sense. Essentially the type of a c-string is actually an array?

You will be like no Ramzi there is no type for a c-string, but I mean conceptually it is an array. If you assign a c-string an identifier, that identifier is linked to an array of char.

Yes: colloquially when people say that some function is working with a "C string", it is typically working with a pointer to a character that is an element of an array of characters, which is null-terminated. The C++ string's member function that returns such a pointer is actually called "c_str()"
Wait a second.. If a string literal is an array of char and a C string is also an array of char what is the difference between the two?

One has a identifier and one does not?

Also if a C-string has no type i.e is not officially defined as a class, then how can it have a member function?

Is the member function not actually a member function but just a function that is called for arrays of char?

As in due to the ideology of classes on entering onto the scene with c++ and the libraries for relating to c-string being based in C, the original functions for manipulating c strings were not conceptually labeled as member functions but you are referring to them as such to ease my understanding?

Apologies for my incessant curiosity.
Last edited on
A string literal is an array of char, but it's not necessary holding a C string. For example, "a\0b" is an array of 4 char, which is not a C string. The first two chars in it are a C string, and the second two chars are a different C string. If you use strlen() or strcpy() with this literal, you will be working with the first C string.
C strings have no member functions, there are many functions in the <cstring> header that take char* arguments which they expect to be pointing to c strings.
The only member function I mentioned was a member function of the C++ string.
Topic archived. No new replies allowed.