Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>z</code> is a pointer to a pointer (which will typically point to a dynamically allocated array of pointers).</p> <p><code>y</code> is a pointer to <code>int</code>. Again, more often than not it'll point to a dynamically allocated array of <code>int</code>s.</p> <p>So, the <code>*z = &amp;x;</code> is setting the pointer that <code>z</code> refers to to point at <code>x</code>. I.e., <code>z</code> points at a pointer, which (in turn) points to <code>x</code>.</p> <p><code>*y = x;</code> is taking the <em>value</em> of <code>x</code> and assigning it to the <code>int</code> pointed to by <code>y</code>.</p> <p>For things like this, a picture is often helpful. So, our basic definitions give us this:</p> <p><img src="https://i.stack.imgur.com/yIJ4J.png" alt="enter image description here"></p> <p>The we do:</p> <pre><code>z = (int **) malloc (sizeof(int *)); y = (int *) malloc (sizeof(int)); </code></pre> <p>Which gives us this:</p> <p><img src="https://i.stack.imgur.com/rdqD5.png" alt="enter image description here"></p> <p>Then we do:</p> <pre><code>*z = &amp;x; *y = x; </code></pre> <p>Which gives us this:</p> <p><img src="https://i.stack.imgur.com/b0X5n.png" alt="enter image description here"></p> <p>In all of these, a dashed line signifies a pointer from one place to another, while the solid line indicates copying a value from one place to another.</p> <p>We can then consider the long-term differences between them. For example, consider what happens if we add <code>x=2;</code> after all the assignments above. </p> <p>In this case, <code>*y</code> will still equal <code>1</code>, because we copied the value <code>1</code> from <code>x</code> to <code>*y</code>. <code>**z</code> will equal <code>2</code> though, because it's just a pointer to <code>x</code> -- any change in <code>x</code> will be reflected in <code>**z</code>.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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