0

I manually assigned characters from a string to another array of string. But why it results in having 0 length and cannot be directly printed? although the characters in the string can be printed by iterating.

Code:

#include <iostream>
#include <string.h>
using namespace std;

int main() {

    string domain[100],text="google";
    for(int i=0;i<6;i++){
        domain[0][i] = text[i];
    }
    for(int k=0;k<6;k++){
        cout<<domain[0][k];
    }
    cout<<endl;
    cout<<"String length: "<<domain[0].length()<<endl;
    cout<<"Output string directly from array : "<<domain[0]<<endl;
    return 0;
}

Output :

google
String length: 0
Output string directly from array : 
user3411184
  • 71
  • 1
  • 10

2 Answers2

3

By writing

 string domain[100]

you create an array of 100 empty strings then you try to access string characters by using [] operator, but the string has no characters, so setting them won't work. You shouldn't even try to access them this way. It causes Undefined Behavior.

EDIT.

In this line:

domain[0][i] = text[i];

you try to set the i-th character of the first string. But how many characters this string has? None.

It would be the same as if you'd try something like this:

domain[0] = "abc";

Then you can access domain[0][0] but can't access domain[0][3] because it has only 3 characters. Try to think of it as of an array. You cannot set 10-th element if the array has only 5 elements.

Estiny
  • 888
  • 1
  • 7
  • 20
  • I don't understand. can you please explain more? since domain[0][0] domain[0][1] ... domain[0][5] each has a character. what is the difference between printing it with cout< – user3411184 Sep 09 '15 at 10:49
  • 1
    @user3411184 `domain[0]` is empty, so `domain[0][0]` isn't valid. – TartanLlama Sep 09 '15 at 10:50
  • 2
    The best solution is to get an [introductory book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and read it. – TartanLlama Sep 09 '15 at 10:52
  • oh I see, the last edited post make me understand. thanks a lot! – user3411184 Sep 09 '15 at 10:58
2
string domain[100];

You now have an array of 100 empty string objects.

(Why? You are only ever using the first one...)

for(int i=0;i<6;i++){
    domain[0][i] = text[i];
}

On first iteration, string::operator[] will return a reference to CharT() (the null character). Modifying that character is undefined behaviour. (Since C++11; earlier versions would invoke undefined behaviour at the operator[] already.)

Note that, in C++, the null character is not really a part of the string. This is different from C's char[].

So your modifying the null character was undefined behaviour and did not actually add a character to the string object. On second iteration, there still is no character in the string, so domain[0][1] is an out-of-bounds access (which is also undefined behaviour).

You are probably looking for string::operator+=() or string::push_back()...


Unrelated:

#include <string.h>

That's the wrong header. C++ strings are in <strings>, <string.h> is the now-deprecated way of including the C string header (the one with strcpy() et al.). If you actually want that, include <cstring>, which introduces the names from <string.h> in the std:: namespace.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • On first iteration, string::operator[] will return a reference to CharT() (the null character). Does it depend on implementation? – songyuanyao Sep 09 '15 at 10:54
  • @songyuanyao: Not really, it's in the standard definition. See the link. Before C++11, the `operator[]` call would be UB even on the first iteration, since `domain[0]` is not `const`. – DevSolar Sep 09 '15 at 10:56
  • Thank you for the answer but I quite understand more from the other answer's explanation. anyway I'll try the push_back() thanks! – user3411184 Sep 09 '15 at 10:59
  • @DevSolar Could you tell me the definition's position in standard? – songyuanyao Sep 09 '15 at 11:01
  • @songyuanyao: No, I don't have the C++ standard available. I am relying on the linked documentation. – DevSolar Sep 09 '15 at 11:06