Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I understood multi-dimensional arrays as pointers to pointers, but perhaps I am wrong?</p> </blockquote> <p>Yes, you are wrong. There is a difference between an array and a pointer. An array can decay into a pointer, but a pointer doesn't carry state about the size or configuration of the array to which it points. Don't confuse this automatic decay with the idea that arrays and pointers are the same -- they are not.</p> <p>A <code>char **</code> is a pointer to a memory block containing character pointers, which themselves point to memory blocks of characters. A <code>char [][]</code> is a single memory block which contains characters.</p> <p>If you have a <code>char **</code> and access it using <code>ptr[x][y]</code>, the compiler changes that into <code>*(*(ptr + x)+y)</code>. If you have a <code>char [][]</code>, the compiler changes <code>arr[x][y]</code> into <code>*(ptr + rowLength*y + x)</code>. (<em>Note</em>: I'm not 110% positive on the order of Xs and Ys here, but that doesn't matter for the point I'm making here) Note that given the pointer, the compiler doesn't know anything about the size or dimensions of the array, and cannot determine the actual address if you treat the pointer as a multidimensional array.</p> <p><code>char *dic[][40]</code> is <a href="http://cdecl.ridiculousfish.com/?q=char++*dic[][40]" rel="noreferrer">an array of arrays of size forty, which contain character pointers</a>. Therefore it doesn't match your assignment there at all.</p> <p><code>p = (char **)dic;</code> &lt;-- This is why casts are bad. The compiler was telling you that what you really wanted to do to <code>dic</code> didn't make any sense. But since you can cast a pointer to any other pointer, the cast succeeds, even though trying to read the data that way will result in undefined behavior.</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