Thinking Inside the "Box"
About 10 minutes ago, my brain switched from neutral to first gear and started pondering about ways to teach someone how to program (Not that I am particularly good at programming in the first place :D).
For example, how would you explain the concept of variables, references, names. etc, which seemingly form the corner stone of programming language syntax. Sometimes this can make you struggle a bit when you are switching languages.
Consider the following,
1 2 3 4 5 6
One teency weency thing I learnt the hardway was the fact that when the interpreter evaluates ‘a = 10’, it creates a “box”, slaps the label ‘a’ on it and puts the integer 10 in it. Consequently, something like swapping of two numbers becomes as easy as,
This is actually emphasised on most, if not all Python tutorials I have come across. This didn’t sound all that important when I learnt Python for the first time.
Then I found C to be doing things differently (Am I even right?), Take this for example,
1 2 3 4 5 6 7 8 9 10 11 12 13
Gives the following output, where x and y are named on different boxes and yet have the same values.
Hmm, This is where I hit it. Suddenly realisation dawned. Sun shone again. Birds twittered. The darkness was gone. I suddenly found myself telling “Its called the pointers.. stupid!”
Now, to mirror the the Python “box” analogy (not exactly) well, a in a language like C, we have to our disposal the good old pointers.
1 2 3 4 5 6 7 8 9 10 11 12
This would give me,
Value of x = 5, Address of x is c07ec844
Value of y = 5, Address of y is c07ec844
where ‘*’ and ‘&’ are the de-reference (value-of) and reference (value-at) operators.
So folks, Is it right to think along the lines of “a box at c07ec844 is labelled ‘x’ with the integer value 5” when I see something as mundane as “int x = 5” . It did help me to understand pointers better, but are there any better analogies when speaking about variables, references etc. than say, picturing a row of shelves with lots of boxes, each of which can have multiple sticky notes with names at a given point of time.