Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think it is worth adding to the other answers a quick explanation of pointers, arrays and memory locations in c.</p> <p>Firstly arrays in c are just a block of memory big enough to hold the number of items in the array (see <a href="http://www.cplusplus.com/doc/tutorial/arrays/">http://www.cplusplus.com/doc/tutorial/arrays/</a>)</p> <p>so if we said</p> <pre><code>int[5] example; example[0] = 1; example[1] = 2; example[2] = 3; example[3] = 4; example[4] = 5; </code></pre> <p>Assuming int is 32 bits we would have a block of memory 5*32bits = 160bits long. As C is a low level language it tries to be as efficient as possible, therefor stores the least amount of information about arrays as possible, in this case the least amount possible is the memory address of the first element. So the type of example could be expressed as</p> <pre><code>int *example; </code></pre> <p>Or example points to an int. To get the items in the array you then add the correct number to the address stored in example and read the number at that memory address. If we assumed memory look like</p> <pre><code>Memory Address = Value (ints take up 4 bytes of space) 1000 = 1 &lt;-- example 1004 = 2 1008 = 3 1012 = 4 1016 = 5 </code></pre> <p>So</p> <pre><code>int i = example[3]; //The 4th element </code></pre> <p>could be expressed as</p> <pre><code>int i = *(example + 3 * sizeof(int)); int i = *(example + 3 * 4); int i = *(1000 + 12); int i = *(1012); // Fetch the value at memory location 1012 int i = 4; </code></pre> <p>The sizeof(int) is 4 (int is 32 bits, or 4 * 8 bit bytes). If you where trying to do addition you would want a char which is 8 bits or 1 * 8 bit bytes.</p> <p>So back to you code</p> <pre><code>char* p; // declare p as a pointer to a char/ p = (char *)a; // point p at memory location 3000 // p[b] would be the 21st element of the "array" p =&gt; // p[20] =&gt; // p + 20 * sizeof(char) =&gt; // p + 20 * 1 =&gt; // p + 20 =&gt; // 3000 + 20 =&gt; // 3020 // the &amp; operator in c gets the address of the variable so sum = (int) &amp;p[b]; // &amp;p[b] =&gt; find the address pointed to by p[b] =&gt; 3020 // (int) casts this pointer to a int. </code></pre> <p>So sum is assigned the address of the 21st element of the array.</p> <p>Long winded explanation.</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.
 

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