Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ vector of strings, why assignment results in same char references?
    primarykey
    data
    text
    <p>I am currently learning C++, and I wanted to manually input values to a vector of strings, processing each character before storing it. This is the code I used:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; using namespace std; int main() { int i, height = 5, width = 5; vector&lt;string&gt; lab; lab.resize(height, ""); string s; //For input reference cout &lt;&lt; endl &lt;&lt; "Input data:" &lt;&lt; endl &lt;&lt; "01234" &lt;&lt; endl; //Input values for (i = 0; i &lt; height; i++) { getline (cin, s); for (int j = 0; j &lt; width; j++) { //Process char [...] lab[i][j] = s.at(j); } } //Show Matrix cout &lt;&lt; endl &lt;&lt; endl; for (int i = 0; i &lt; height; i++) { for (int j = 0; j &lt; width; j++) { cout &lt;&lt; lab[i][j] &lt;&lt; " "; } cout &lt;&lt; endl; } //Show Addresses cout &lt;&lt; endl &lt;&lt; endl; for (int i = 0; i &lt; height; i++) { cout &lt;&lt; "String Addr: " &lt;&lt; (void*) &amp;lab[i] &lt;&lt; " | Chars Addr: "; for (int j = 0; j &lt; width; j++) { //cout &lt;&lt; lab[i][j] &lt;&lt; " "; cout &lt;&lt; (void*) &amp;lab[i][j] &lt;&lt; " "; } cout &lt;&lt; endl; } cout &lt;&lt; endl; return 0; } </code></pre> <p>The problem was that the characters inside the strings have the same address (I don't know why). Here is an example of one execution of the program:</p> <pre><code>Input data: 01234 aaaaa bbbbb ccccc ddddd abcde a b c d e a b c d e a b c d e a b c d e a b c d e String Addr: 0x8fe8008 | Chars Addr: 0xb773260c 0xb773260d 0xb773260e 0xb773260f 0xb7732610 String Addr: 0x8fe800c | Chars Addr: 0xb773260c 0xb773260d 0xb773260e 0xb773260f 0xb7732610 String Addr: 0x8fe8010 | Chars Addr: 0xb773260c 0xb773260d 0xb773260e 0xb773260f 0xb7732610 String Addr: 0x8fe8014 | Chars Addr: 0xb773260c 0xb773260d 0xb773260e 0xb773260f 0xb7732610 String Addr: 0x8fe8018 | Chars Addr: 0xb773260c 0xb773260d 0xb773260e 0xb773260f 0xb7732610 </code></pre> <p>At the end you can see that the addresses of the chars are the same, while the ones for the strings are different. The final matrix in the example should have been:</p> <pre><code>a a a a a b b b b b c c c c c d d d d d a b c d e </code></pre> <p>But instead, it is:</p> <pre><code>a b c d e a b c d e a b c d e a b c d e a b c d e </code></pre> <p>While I already solved the problem, using the <code>+=</code> operator instead of assigning using <code>[]</code>, I still don't know exactly what hapened with the code above. Why does the chars have the same reference?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload