Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The first makes a variable named <code>arr</code> that is of an array length 5 of type <code>int*</code>, so it is an array length 5 of int pointers. </p> <p>The second declares a variable named <code>arr</code> that is a pointer to an array length 5 of type int. So it is a pointer to an array of ints length 5.</p> <p>The third is a variable named arr that is a pointer to an array of length 5 of type int pointer. So it is a pointer to an array length 5 of int pointers. Or, a pointer to the type of the first case! </p> <p>To think of how to analyze things like this in the future, realize that parenthesis have the highest priority here so by having <code>(*arr)</code> it is first, and foremost, a pointer. Then the array notation has second highest priority, so <code>(*arr)[5]</code> is a pointer to an array of length 5...but what type of array? Well the type is the lowest priority, so that comes last. If we have int <code>(*arr)[5]</code>, then it is a pointer (1st) to an array length 5 (2) of type int (3). </p> <p>With that we can look at something like: <code>int** (*arr)[7]</code> It is a pointer named <code>arr</code> (1) to an array length 7 (2) of type <code>int**</code>, or pointers to pointers of ints (3). </p> <p>As far as wondering how pointers/arrays are represented in C...</p> <p>pointers and arrays are both 32 bit variables (like ints on most systems) that contain a 32 bit address to a memory location (where they point). Arrays obviously point to the beginning of an array, so <code>*arr == arr[0]</code>. While pointers can point to anything. When you declare an array (<code>arr[5]</code>, <code>arr[1000]</code>, etc.) it makes a pointer named <code>arr</code> as well as reserves whatever space is in between the brackets (5 or 1000). But when you declare a pointer <code>(*arr)</code>, it does not reserve any space. Otherwise, the variable arr in both cases is simply a pointer. Also, if you want to be confusing, you could even dereference your pointers like so: <code>pointer[0]</code>, instead of <code>*pointer</code>. </p> <p>As a final note example, we can look at something like: <code>int * (*arr)[5][7]</code> and analyze both what it is and what the compiler is doing:</p> <p>First, it is a pointer named <code>arr</code> (1), to an array length 5 (2) each to an array length 7 (3) but because an array length n is just a pointer to a reserved location in memory, so really (2) is an array length 5 to a pointer (pointing to memory blocks of size 7). Finally, each of the memory blocks of size 7 (3) is of type <code>int *</code> (4). </p> <p>So, it is a pointer to an array length 5 of pointers to arrays of length 7 of type <code>int *</code>!</p> <p>I hope this helps! I know I had a lot of fun playing with C and seeing what was possible and what different things did =)</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