Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think the best approach is to introduce 1 concept at a time. You don't need to 100% explain arrays in the first module. You can detangle almost anything by introducing 1 concept at a time. </p> <p>I would teach them in this order: Arrays, Pointers, Arrays+Pointers, OtherStuff[N].</p> <hr> <p><strong>Arrays:</strong></p> <p>You can teach simple arrays first so they understand the ability to have multiple data slots accessible from a single variable name.</p> <pre><code>//The following doesn't need an understanding of pointers int x[10]; x[0] = 5; </code></pre> <hr> <p><strong>Pointers:</strong></p> <p>Then you can teach about pointers and how they work, starting with some simple examples:</p> <pre><code>int y = 5; int *p = &amp;y; *p = 6; printf("%i\n", y); </code></pre> <p>Make sure to give a special emphasis that a pointer is just like any other variable. It stores a memory address. </p> <p>There is no need to get into the stack vs heap just yet.</p> <hr> <p><strong>Arrays+Pointers:</strong></p> <p>How to iterate over arrays with pointers:</p> <pre><code>int x[10]; x[0] = 5; x[1] = 6; int *y = x; printf("%i\n", *y);//prints the first element y++; printf("%i\n", *y);//prints the second element </code></pre> <hr> <p><strong>Then you can teach more complicated things...</strong></p> <ul> <li>How to do pointer arithmetic.</li> <li>Array + i shorthand for array[i]</li> <li><a href="https://stackoverflow.com/questions/671074/why-c-or-c-does-not-allow-passing-array-by-values-to-function/671076#671076">Passing arrays to functions as array pointets vs pointer param + size param</a></li> <li>How arrays are continuous blocks of memory</li> <li>Explain string literals, buffers, ...</li> <li>How sizeof works with pointers vs array types (pointer size vs buffer size)</li> <li>Explain more complicated concepts like allocating memory, <a href="https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap/79936#79936">the stack, and the heap</a></li> <li>Multiple levels of indirection</li> <li>References</li> <li>How multi-dimensional arrays work</li> <li>...</li> </ul> <p>Throughout all examples make heavy use of sizeof and printing addresses. It really helps to understand what's going on. </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