Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When teaching C, is it better to teach arrays before or after pointers? [closed]

Tags:

arrays

c

pointers

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.

like image 704
Uri Avatar asked Mar 22 '09 00:03

Uri


People also ask

Is a pointer to a block of memory effectively the same as an array?

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.

How pointers are better than arrays in C?

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.

Which is faster arrays or 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.

Which is better pointer or array?

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.


1 Answers

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...

  • How to do pointer arithmetic.
  • Array + i shorthand for array[i]
  • Passing arrays to functions as array pointets vs pointer param + size param
  • How arrays are continuous blocks of memory
  • Explain string literals, buffers, ...
  • How sizeof works with pointers vs array types (pointer size vs buffer size)
  • Explain more complicated concepts like allocating memory, the stack, and the heap
  • Multiple levels of indirection
  • References
  • How multi-dimensional arrays work
  • ...

Throughout all examples make heavy use of sizeof and printing addresses. It really helps to understand what's going on.

like image 159
10 revs Avatar answered Oct 13 '22 05:10

10 revs