In C , the string literal, “I love you.” can be coded as follows:

char arrStr[] = {‘I’, ‘ ‘, ‘l’, ‘o’, ‘v’, ‘e’, ‘ ‘, ‘y’, ‘o’, ‘u’, ‘.’, ,‘a’, ‘n’, ‘y’, ‘t’, ‘h’, ‘i’, ‘n’, ‘g’, ‘ ‘, ‘e’, ‘l’, ‘s’, ‘e’};

Characters in an array of chars, ending with the nul character, is a string. The array above actually contains the phrases, “I love you.” and “anything else” separated by the character, ‘’.

would print:

ignoring anything else. This is the traditional way of having a string in C . Anything else should be ignored after the ‘’ character if the array content is to be considered a string.

With the pointer, the above string would be coded as:

const char* ptrStr = “I love you.”;

and

would print:

An array of characters is a constant pointer to characters, ending with ‘’. This explains why const is used in the statement, “const char* ptrStr = “I love you.”;”. The double quotes eliminate the use of the array literal structure and ‘’.

With the string class, a string object for the above phrase would be

string objStr = string(“I love you.”);

and

would print:

The string object could still have been instantiated as,

string objStr = string({‘I’, ‘ ‘, ‘l’, ‘o’, ‘v’, ‘e’, ‘ ‘, ‘y’, ‘o’, ‘u’, ‘.’, });

The indirect question is how to convert an array string literal to a literal double quote and how to convert an array literal or double quote literal to a string object. The direct question is, when these values are elements of the vector, how to do these conversions. This article explains that.

Before getting into the core explanation, remember that ‘E’ is a character, while “E” is a string. In order to use vectors of string objects, the program should begin with:

#include

#include

#include

using namespace std;

Article Content

Vector of Array Chars To Vector of Pointer Chars

This section explains how to convert a vector of character arrays that form strings to a vector of constant-pointer-to-strings.

Now,

char arrStr[] = {‘I’, ‘ ‘, ‘l’, ‘o’, ‘v’, ‘e’, ‘ ‘, ‘y’, ‘o’, ‘u’, ‘.’, ,};

and

const char* ptrStr = “I love you.”;

mean the same thing inside, as the following code shows:

char arrStr[] = {‘I’, ‘ ‘, ‘l’, ‘o’, ‘v’, ‘e’, ‘ ‘, ‘y’, ‘o’, ‘u’, ‘.’, ,};

for (int i=0; arrStr[i] != ; i )


    cout << arrStr[i];

cout << endl;

const char* ptrStr = “I love you.”;

for (int i=0; ptrStr[i] != ; i )


    cout << ptrStr[i];

cout << endl;

The output is:

All code segments for this article are in the main() function body. For the array, the array name with [i] is used to read all the values in the array. The pointer name with [i] is used to read all the values in the string literal for the pointer. Note that ‘’ is implicit at the end of the literal string. What is constant for both cases is the pointer and not the value. An array name is a constant pointer to the sequence of characters, which should end with ‘’.

So, a vector of arrays of chars, where each array ends with ‘’ or a vector of double-quote string literals, should be declared in the same way, as follows:

Consider the following vector of fruit names, where each fruit name is an array of chars, ending with ‘’.

char fruit1[] = {‘p’,‘a’,‘p’,‘a’,‘y’,‘a’,};

char fruit2[] = {‘s’,‘t’,‘r’,‘a’,‘w’,‘b’,‘e’,‘r’,‘r’,‘y’,};

char fruit3[] = {‘p’,‘a’,‘s’,‘s’,‘i’,‘o’,‘n’,‘ ‘,‘f’,‘r’,‘u’,‘i’,‘t’,};

char fruit4[] = {‘b’,‘a’,‘n’,‘a’,‘n’,‘a’,};

char fruit5[] = {‘o’,‘r’,‘a’,‘n’,‘g’,‘e’,};

vector<const char*> vtr {fruit1, fruit2, fruit3, fruit4, fruit5};

The vector of fruits has been constructed by writing the array names as elements in the vector. This same vector can be constructed with string literals as follows:

vector<const char*> vtr = {“papaya”, “strawberry”, “passion fruit”, “banana”, “orange”};

So, there is no need to convert a vector of arrays-of-chars to a vector of const-pointers-to-chars. They are the same thing, underneath. Since they are the same thing, an array string value can be read into a const-pointer-to-chars, as the following code shows:

char fruit1[] = {‘p’,‘a’,‘p’,‘a’,‘y’,‘a’,};

char fruit2[] = {‘s’,‘t’,‘r’,‘a’,‘w’,‘b’,‘e’,‘r’,‘r’,‘y’,};

char fruit3[] = {‘p’,‘a’,‘s’,‘s’,‘i’,‘o’,‘n’,‘ ‘,‘f’,‘r’,‘u’,‘i’,‘t’,};

char fruit4[] = {‘b’,‘a’,‘n’,‘a’,‘n’,‘a’,};

char fruit5[] = {‘o’,‘r’,‘a’,‘n’,‘g’,‘e’,};

vector<const char*> vtr {fruit1, fruit2, fruit3, fruit4, fruit5};

for (int i=0; i<vtr.size(); i ) {


   const char* str = vtr[i];


   cout << str << “, “;

}

cout << endl;

The output is:

papaya, strawberry, passion fruit, banana, orange,

The line,

const char* str = vtr[i];

is where the supposed conversion takes place.

Vector of Pointer to Chars To Vector of String Objects

The question of converting a vector of pointer-to-chars to vector-of-string-objects, is the same as the question of converting a vector of arrays-of-chars to vector-of-string-objects. Consider the following statement:

vector<const char*>


vtr = {“papaya”, “strawberry”, “passion fruit”, “banana”, “orange”};

The following declaration has the above declaration, in string object form:

vector<string>


vtr = {string(“papaya”), string(“strawberry”), string(“passion fruit”), string(“banana”), string(“orange”)};

In this case, “#include ” has to be at the top of the program. Notice the template argument and the string object values.

Now, it is possible to assign a string literal, to become the content of a string object, as the following three code segments show:

string str = “abc”;

const char* strLit = “abc”;


string str = strLit;

char arr[] = {‘a’,‘b’,‘c’,};


string str = arr;

With this knowledge, each string literal can be read into a string object variable, as the following code shows:

vector<const char*> vtr = {“papaya”, “strawberry”, “passion fruit”, “banana”, “orange”};

for (int i=0; i<vtr.size(); i ) {


    string str = vtr[i];


    cout << str << “, “;

}

cout << endl;

The output is:

papaya, strawberry, passion fruit, banana, orange,

The line that does the conversion from literal to string object is:

If the vector values were array strings, then the following code will do the same thing:

char fruit1[] = {‘p’,‘a’,‘p’,‘a’,‘y’,‘a’,};

char fruit2[] = {‘s’,‘t’,‘r’,‘a’,‘w’,‘b’,‘e’,‘r’,‘r’,‘y’,};

char fruit3[] = {‘p’,‘a’,‘s’,‘s’,‘i’,‘o’,‘n’,‘ ‘,‘f’,‘r’,‘u’,‘i’,‘t’,};

char fruit4[] = {‘b’,‘a’,‘n’,‘a’,‘n’,‘a’,};

char fruit5[] = {‘o’,‘r’,‘a’,‘n’,‘g’,‘e’,};

vector<const char*> vtr {fruit1, fruit2, fruit3, fruit4, fruit5};

for (int i=0; i<vtr.size(); i ) {


    string str = vtr[i];


    cout << str << “, “;

}

cout << endl;

The output is the same:

papaya, strawberry, passion fruit, banana, orange,

The line that does the conversion from literal to a string object is still the same:

Vector of String Literals to Vector of String Objects

To really change a vector of string literals to a vector of string objects, the following procedure will have to be followed:

  • Create another empty vector, but this time, a vector of string objects.
  • Copy each string literal from the vector of const-character-pointers, to the vector of string objects, by pushing.
  • Destroy the old vector of literals.

The following code illustrates this:

vector<const char*> vtr = {“papaya”, “strawberry”, “passion fruit”, “banana”, “orange”};


vector<string> vtrNew;

for (int i=0; i<vtr.size(); i ) {


    vtrNew.push_back(vtr[i]);

}

vtr.~vector();

for (int i=0; i<vtrNew.size(); i ) {


    string str = vtrNew[i];


    cout << str << “, “;

}

cout << endl;

The output is:

papaya, strawberry, passion fruit, banana, orange,

The line for the code that destroys the old vector is:

The content of the vector is destroyed, but not the vector name. However, the old vector name cannot be reused (in the same scope).

Conclusion

An array string literal and a constant pointer to character sequence literal are the same things underneath. An array string literal is an array literal of chars, ending with ‘’. A const-pointer-to-char-sequence literal sequence of consecutive characters delimited by double quotes, e.g., “abc”. ‘’ is implicit at the end of the const-pointer-to-char-sequence literal.

An array string literal or a const-pointer-to-char-sequence literal can be assigned to the identifier of a const-pointer-to-char-sequence, as illustrated in the following code segment:

char arr[] = {‘a’, ‘b’, ‘c’, };

const char* ss = “def”;


ss = arr;

const char* sss = ss;

There is no really need to convert a vector of array strings to a vector of string literals in double-quotes. It suffices to read each array string of the vector to the identifier of a const-pointer-to-char-sequence.

However, if a vector of string objects is really required from a vector of string literals, then the conversion should be done as follows:

  • Create another empty vector, but this time, a vector of string objects.
  • Copy each string literal from the vector of const character pointers to the vector of string objects by pushing.
  • Destroy the old vector of literals.

Destroying a vector means destroying its content (elements), but not the name of the vector.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/Chrysanthus-150×150.jpeg" height="112" src="data:image/svg xml,” width=”112″>

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.