Note that there are some explanatory texts on larger screens.

plurals
  1. POC style strings, Pointers, arrays
    primarykey
    data
    text
    <p>I'm having trouble understanding what a C-style string is. Happy early New Year</p> <p>What I know: A pointer holds a memory address. Dereferencing the pointer will give you the data at that memory location.</p> <pre><code>int x = 50; int* ptr = &amp;x; //pointer to an integer, holds memory address of x cout &lt;&lt; "&amp;x: " &lt;&lt; &amp;x &lt;&lt; endl; //these two lines give the same output as expected cout &lt;&lt; "ptr: " &lt;&lt; ptr &lt;&lt; endl; cout &lt;&lt; "*ptr: " &lt;&lt; dec &lt;&lt; (*ptr) &lt;&lt; endl; //prints out decimal number 50 //added dec, so the program doesnt //continue to printout hexidecimal numbers like it did for the //the memory addresses above cout &lt;&lt; "&amp;ptr: " &lt;&lt; &amp;ptr &lt;&lt; endl; //shows that a pointer, like any variable, //has its own memory address </code></pre> <p>Now to what I don't understand (using what's above as the source for my confusion): There are multiple ways to declare strings. I'm learning C++, but you can also use C-style strings (good to understand, although inferior to C++ strings)</p> <h3>C++:</h3> <pre><code>string intro = "Hello world!"; //the compiler will automatically add a null character, \0, so you don't have to //worry about declaring an array and putting a line into it that is bigger than //it can hold. </code></pre> <h3>C-style:</h3> <pre><code>char version1[7] = {'H','i',' ','y','o','u','\0'}; char version2[] = "Hi you"; //using quotes, don't need null character? added for you? char* version3 = "Hi you"; </code></pre> <p>Version3 is where I'm having trouble. Here, there is a pointer to a char. I know that an array name is a pointer to the first element in an array.</p> <pre><code>cout &lt;&lt; " &amp;version3: " &lt;&lt; &amp;version3 &lt;&lt; endl; //prints out location of 'H' cout &lt;&lt; " *version3: " &lt;&lt; *version3 &lt;&lt; endl; //prints out 'H' cout &lt;&lt; " version3: " &lt;&lt; version3 &lt;&lt; endl; //prints out the whole string up to //automatically inserted \0 </code></pre> <p>Before, in the section "what I knew," printing out the name of a pointer would print out the address that it held. Here, printing out the name of a pointer prints out the whole string. Do the double quotes around "Hi you" somehow tell the program: "hey I know you are a pointer, and you are initialized to the location of 'H', but because I see these double quotes, skip forward 1 byte in memory location and printout everything you see until you reach a \0" (1 byte movement because char's are 1 byte large). </p> <p>How is it that printing out a pointer prints out a string? Before printing out a pointer name printed out the memory address it was initialized to.</p> <p>Edit: Does <code>cout &lt;&lt; &amp;version3</code> print out the location of 'H' or the location of the pointer, <code>version3</code>, which holds the memory address of 'H'?</p>
    singulars
    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.
 

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