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.
4) A pointer to a block of memory is effectively same as an array. The correct option is (a). Explanation: Using the standard library function malloc() and treat it as an array.
Array in C is used to store elements of same types whereas Pointers are address varibles which stores the address of a variable. Now array variable is also having a address which can be pointed by a pointer and array can be navigated using pointer.
Why ? pointers because it is direct memory access followed by dereferencing array - add current index to base address then dereferencing. To be done for each index.
An array is a collection of elements of similar data type whereas the pointer is a variable that stores the address of another variable. An array size decides the number of variables it can store whereas; a pointer variable can store the address of only one variable in 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.
//The following doesn't need an understanding of pointers
int x[10];
x[0] = 5;
Pointers:
Then you can teach about pointers and how they work, starting with some simple examples:
int y = 5;
int *p = &y;
*p = 6;
printf("%i\n", y);
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:
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With