Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Might be related aswell: <a href="https://stackoverflow.com/questions/13551511/char-why-is-there-no-address-in-the-pointer?rq=1">char* - Why is there no address in the pointer?</a></p> <p>As people have said the problem you're having is that you're hitting the </p> <pre><code>ostream&amp; operator&lt;&lt;(ostream&amp; o, const char* str); </code></pre> <p>output operator which will print the c-string out for you, if you cast it to <code>void*</code> it will instead print the address just as you expected. As far as i can tell you've understood it correctly. One way you can think of pointers to pointers are arrays of arrays, with that thought in mind your example could be rewritten to something like this (this is not totally equivalent though):</p> <pre><code>int main() { char arr1 [] = "hello"; char arr2 [] = "dear"; char arr3 [] = "world"; char p1[3][10]; p1[0] = arr1; //or *(p1+1) = arr1 p1[1] = arr2; //or *(p1+2) = arr2 p1[2] = arr3; //or *(p1+3) = arr3 char** ptr = p1; } </code></pre> <p>I chose 10 just to be able to fit in the longest of the strings and a few extra chars. This would look something like this in memory:</p> <pre><code>hello\0----dear\0-----world\0---- ^ ^ ^ | | | | | arr3,ptr[2] | arr2,ptr[1] *ptr/ptr[0], arr1 </code></pre> <p>Where the dashes are just garbled data which can be anything because we chose the size 10 for our <code>char p1[3][10];</code>. Hopefully the image gives a little more info on how it looks and where the pointers go. Although, if we would have used <code>malloc()</code> to create this structure we could have done it in a few different ways, for example:</p> <pre><code>char* a = (char*)malloc(10*sizeof(char)); char* b = (char*)malloc(10*sizeof(char)); char* c = (char*)malloc(10*sizeof(char)); strcpy(a, "hello"); strcpy(b, "dear"); strcpy(c, "world"); char** ptrs = (char**)malloc(3*sizeof(char*)); //3 pointers, ie: char* ptr[3]; ptrs[0] = a; ptrs[1] = b; ptrs[2] = c; </code></pre> <p>This could look something like this:</p> <pre><code>ptrs:---|, --|, ------| v v v a-\ b-----\ c----\ v v v "hello" "dear" "world" </code></pre> <p>Ie: They wouldn't be laid out as a long line anymore but each and every thing we've allocated would be in it's own place. So <code>ptrs</code> has pointers to a, b and c, while they in turn have pointers to the start of the strings "hello", "dear" and "world". So if we dereference <code>ptrs</code> (<code>*ptrs</code>) we'll get a pointer to <code>a</code> (a <code>char*</code> as we dereference a <code>char**</code>) which is a pointer to the first letter in "hello". When this is sent to <code>cout</code> it finds the forementioned <code>operator&lt;&lt;</code> for c-strings and therefore prints out the string it found. Something that's probably important to mention here is that <code>*ptrs</code> is equivalent to <code>ptrs[0]</code> but it would seem you've already grasped that.</p> <p>And hopefully i haven't totally screwed this up by feeding you bogus information, if anyone sees something that's wrong or if i'm totally mistaken please do tell me so that i can remove this as others have done x) Also i hope this made it clearer in some way, please ask away if ther's anything that's unclear.</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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      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