Note that there are some explanatory texts on larger screens.

plurals
  1. POPointers and Strings C++
    text
    copied!<p>I'm teaching myself C++ and I'm a bit confused about pointers (specifically in the following source code). But first, I proceed with showing you what I know (and then contrasting the code against this because I feel as if there are some contradictions going on).</p> <p>What I know:</p> <pre><code>int Age = 30; int* pointer = &amp;Age; cout &lt;&lt; "The location of variable Age is: " &lt;&lt; pointer &lt;&lt; endl; cout &lt;&lt; "The value stored in this location is: " &lt;&lt; *pointer &lt;&lt; endl; </code></pre> <p>Pointers hold memory addresses. Using the indirection (dereference) operator (the *), you can access what is stored in memory location of the pointer. Onto the code in this book I'm having trouble understanding...</p> <pre><code>cout &lt;&lt; "Enter your name: "; string name; getline(cin, name); //gets full line up to NULL terminating character int CharsToAllocate = name.length() + 1; //calculates length of string input //adds one onto it to adjust for NULL character char* CopyOfName = new char[CharsToAllocate]; // pointer to char's called CopyOfName, is given the memory address of the //beginning of a block //of memory enough to fit CharsToAllocate. Why we added 1? Because char's need a //NULL terminating character (\0) strcpy(CopyOfName, name.c_str()); //copies the string name, into a pointer? cout &lt;&lt; "Dynamically allocated buffer contains: " &lt;&lt; CopyOfName &lt;&lt; endl; delete[] CopyOfName; //always delete a pointer assigned by new to prevent memory leaks </code></pre> <p>Output:</p> <pre><code>Enter your name: Adam Dynamically allocated buffer contains: Adam </code></pre> <p>The comments in the above code are my comments. My problem begins with <code>strcpy</code>. Why is <code>name.c_str()</code> copied into a pointer <code>CopyOfName</code>? Does this mean that all strings are essential pointers? So like string testing = "Hello world"; Is actually a pointer pointing to the memory location where "H" is stored?</p> <p>Next, why is it in the print out statement using <code>CopyOfName</code> and not <code>*CopyOfName</code>? Pointers hold memory addresses? Using <code>*CopyOfName</code> would print out the contents of the memory location. I tried this in Code::Blocks and if the input text was "Hello World." Using <code>*CopyOfName</code> in the print out statement would just give an "H". This makes sense since when I declared that I needed a memory block with the 'new' thing, this actually returns a pointer to the first part of the dynamically allocated memory block.</p> <p>The only way I can reconcile this is if a string is actually a pointer.</p> <pre><code>string testing = "Confused"; cout &lt;&lt; testing &lt;&lt; endl; </code></pre> <p>would print out the word "Confused"</p> <p>However, if I try to compile</p> <pre><code>string testing = "Confused"; cout &lt;&lt; *testing; </code></pre> <p>I get an error message.</p> <p>Basically, to summarize my question, I'm trying to understand the code with <code>strcpy</code> and the <code>cout</code> statement.</p>
 

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