When teaching C, is it better to teach arrays befo

2020-05-20 06:26发布

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.

14条回答
手持菜刀,她持情操
2楼-- · 2020-05-20 06:38

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.

查看更多
小情绪 Triste *
3楼-- · 2020-05-20 06:41

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

查看更多
叼着烟拽天下
4楼-- · 2020-05-20 06:43

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.

查看更多
smile是对你的礼貌
5楼-- · 2020-05-20 06:45

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.

查看更多
神经病院院长
6楼-- · 2020-05-20 06:46

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.

查看更多
ゆ 、 Hurt°
7楼-- · 2020-05-20 06:46

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.

查看更多
登录 后发表回答