For those of you with curriculum development experience: what is the best strategy regarding arrays?
I have seen some schools that teach arrays after variables and control structures, often before even teaching functions. This allows teaching of some rudimentary algorithms, etc. However, it then brings the problem of how to pass arrays to functions, so it is necessary to go back to arrays pointers are taught and patch things up.
Another option is to go from variables and control structures to functions, and then teach pointers, and once you have pointers, teach arrays from scratch, and then use that to get to dynamic memory allocation.
To me the second option makes more sense, because unlike simple variables, with arrays it is easy to "go out of bounds", but students who did not yet learn about memory and pointers may not understand what lies outside these bounds.
However, I'm interested to know what others think.
This question can be asked for any object-oriented language really.
When I was taught Java, I was first shown arrays and the pointers, as the last part of arrays, to demonstrate the difference between a deep copy and a shallow copy.
Would you teach pointers before strings?
Probably not. And most of the same arguments apply.
(But generally I agree with @legion — don't overthink it.)
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.
I would teach them in this order: Arrays, Pointers, Arrays+Pointers, OtherStuff[N].
Arrays:
You can teach simple arrays first so they understand the ability to have multiple data slots accessible from a single variable name.
Pointers:
Then you can teach about pointers and how they work, starting with some simple examples:
Make sure to give a special emphasis that a pointer is just like any other variable. It stores a memory address.
There is no need to get into the stack vs heap just yet.
Arrays+Pointers:
How to iterate over arrays with pointers:
Then you can teach more complicated things...
Throughout all examples make heavy use of sizeof and printing addresses. It really helps to understand what's going on.
I would teach pointers first. They can be explained without teaching arrays. While teaching arrays i could then refer to pointers when explaining the expression
a[i]
, and when explaining how one can pass them to functions.I'm assuming you are teaching C to students who already know how to program in another language like Java (or back in my day, Pascal). I don't think C is a good language to use for teaching programming to complete novices.
I would teach pointers first. This is one of the important new ideas that that will be learning in C. They will already know the concept of arrays from other languages, so there's no urgency to teach this first. So when you do cover arrays in C, you can talk about how they are essentially syntactic sugar for pointer arithmetic, a concept they are now familiar with.
I think it would be better start with arrays, because the concept of array is simple and intuitive, but in C it would be important revisiting arrays after teach ponters, as 'Legion' suggested before.