Arrays are not pointers!

As I have mentioned before, anyone who wants to become proficient in C should own a copy of Deep C Secrets, by Peter Van der Linden (you can find an online copy on archive.org). In it he covers a number of topics which are usually confusing to students, especially the art of pointers. One of the things Mr. Van der Linden discusses is how “arrays are not pointers“. Confused? Well you likely are not alone.

Typically most C programmers are told that “arrays are the same as pointers”. This can be dangerous because it sets the wrong tone, and is really a half-truth. It’s a bit of a chicken and egg problem. Arrays look like pointers, and pointers can refer to array objects.

For example, people sometimes think that char s[] is identical to char *s. But they aren’t identical. The array declaration char s[12] requests that space for 12 characters be set aside, to be known by the name s. The pointer declaration char *p, on the other hand, requests a place that holds a pointer, to be known by the name p. This pointer can point to almost anywhere: to any char, to any contiguous array of chars, or frankly nowhere. To illustrate this better consider the following example the following declarations:

char s[] = "tauntaun";
char *p = "dewback";

would produce structures of the form:

A reference like m[4] then generates different code, depending on whether m is an array, or a pointer. When a compiler sees s[4], it starts at location s[0], moves 4 elements past it, and obtains the character there. When it sees expression p[4], it starts at the location p, obtains the pointer value there, adds 4 to the pointer, and finally obtains the character pointed to.

The reality is that it’s really easy to get confused in C, especially when books say things like “an array is merely a cleverly disguised pointer”. Here is a further example.

char s[] = "tauntaun";
char *p = s;

In this case, p is a pointer to char; it refers to a block of memory large enough to hold the address of a single char object. It is initialized to point to the first element in s. So *p is equivalent to s[0], *(p+1) is the same as s[1], etc.

One of the reasons people get confused is because books say things like “the compiler converts array subscripts into pointer dereferences, s[i] becomes *(s+i)“. Or they go further down the rabbit hole and declare the following are all equivalent:

s[i]
*((s)+(i))
*((i)+(s))
i[s]

Which they are, but it’s not something that novice C programmers should be exposed to. Why are they equivalent? Because array subscripting is defined in terms of pointer operations. If you don’t believe me, then consider what C’s creator Dennis Ritchie has to say about it [1]. Arrays in C’s predecessors, B and BCPL were pointers, as they were in the extended language “new B”. But there were issues, specifically with arrays and the creation of structured (record) types – if they contained an array there was “no convenient place to stash the pointer”.

“The solution constituted the crucial jump in the evolutionary chain between typeless BCPL and typed C. It eliminated the materialization of the pointer in storage, and instead caused the creation of the pointer when the array name is mentioned in an expression. The rule, which survives in today’s C, is that values of array type are converted, when they appear in expressions, into pointers to the first of the objects making up the array.”

Dennis Ritchie [1]

Too many books fail to explain when arrays are pointers and when they are not.

  1. Dennis Ritchie, “The Development of the C Language“, Bell Labs (1993)

The programming problem

An excerpt from “The Programming Problem“, by Richard W. Hamming, Computerworld, Feb.3, 1997.

“I have often used the analogy of novel writing with the writing of software. In both cases, the essence is that clear thinking is followed by the straightforward writing of the actual words. To what extend can great writing be taught?”

Shakespeare and Jane Austin did not take creative writing courses, and most people who take creative writing courses do not end up among the greatest writers. Similarly, all that programming courses can do, apparently, is make poor programmers a bit better. Experience, while necessary, seems not to be enough. And there is little evidence that experience with many programming languages and many years of writing programs makes people much better at programming.

“The programming problem is more a matter of getting rid of the poor programmers than anything else. It has long been true that good programmers outperform poor ones by more than a factor of 10, and the poor ones are a burden on the good ones. The way we teach programming does not in any way resemble how sports and other art forms are taught, and we had better look at how programmers learn to program rather than trying to fix the bad habits they develop in class and on their own. But unfortunately, many of the current professors of programming are themselves poor programmers with no real sense of taste or style. They tend to think it is a matter of teaching cute, accurate, fast programming.”

The “Average Joe” system

A article in Computerworld, March 30, 1987 title “AI, Mozart and the thermos” by Ashley Grayson, described the idea of an “Average Joe System” instead of an “Expert” system. As an example he described a story:

Three construction workers sat down to lunch on the steel girders of a skyscraper they were building. They started talking about the world’s greatest inventions. The first said he thought the light bulb was the greatest invention. “Without the light bulb, we couldn’t do any work at night, except by candlelight, and can you imagine trying to find the way home without streetlights?” The second disagreed. “No, it’s the wheel. Without the wheel, we couldn’t have wheelbarrows, or backhoes, or cars, or trains, or anything! Day or night, we’d have to walk!” he said. “No, no,” the third countered, “It’s the thermos bottle.” “The thermos bottle!” cried the other two. “What’s that got to do with it?” “Well,” replied the third, “it’s artificially intelligent. It keeps your coffee hot and your lemonade cold. How do it know?”

Who are we?

Who are we? We find that we live on an insignificant planet of a humdrum star lost in a galaxy tucked away in some forgotten corner of a universe in which there are far more galaxies than people.

Carl Sagan

Sagan on books

“What an astonishing thing a book is. It’s a flat object made from a tree with flexible parts on which are imprinted lots of funny dark squiggles. But one glance at it and you’re inside the mind of another person, maybe somebody dead for thousands of years. Across the millennia, an author is speaking clearly and silently inside your head, directly to you. Writing is perhaps the greatest of human inventions, binding together people who never knew each other, citizens of distant epochs. Books break the shackles of time. A book is proof that humans are capable of working magic.”

Carl Sagan, Cosmos, Part 11: The Persistence of Memory (1980)

The do’s and don’t of student success

I have been teaching a long time now, and it has become clear that students just aren’t as well prepared for university as they use to be. Getting rid of grade 13 in Ontario certainly didn’t help. Neither do helicopter (or snowplow) parents. Here are some of my observations on things students could do to improve their learning experiences.

  • Don’t ignore course material.
    • Some students don’t spend enough time actually comprehending course material, and this includes emails, and assessment material. Emails are sent out for a reason, but the amount of students who ask questions that have been answered in course emails is sometimes ludicrous. Instructors get tired of answering questions that wouldn’t need to be answered if the student bothered to peruse the course material.
  • Do thoroughly read assignment descriptions.
    • Some students just seem to skim the assignments, and then are miffed when they don’t get the grade expected. If you don’t fulfill the requirements, then it’s not possible to get the grades, nor is it possible to “redo” the assignment. It’s not the instructors fault if a student fails to read an important part of an assignment.
  • Don’t just do your own thing.
    • There is no issue with going beyond what is asked, but assessments exist for a reason. Instructors (usually) spend a lot of time designing them. So don’t ignore this and decide to just do your own thing. If an assignment says “Choose a topic from the list”, and a student chooses to ignore that instruction, it should be no surprise when they get a zero for the assignment. There is nothing more frustrating to an instructor than a student who cannot follow basic instructions.
  • Do manage your time well.
    • Poor time management leads to things being done at the last minute. Yes there are times where things spiral out of control, but if you manage assignment deadlines, you will get it done well before the deadline. Leaving it to the last minute means you might be tempted to ask for an extension at the 11th hour…
  • Don’t ask for extensions.
    • The only people who should be asking for extensions are those who are suffering from some medical issue. People who ask for extensions at the last minute for other reasons are usually denied. Why? Because if you have had 3-4 weeks to do an assignment, few instructors have much patience for last minute requests like: “because I ran out of time”, “because my computer crashed”, “because I was on holiday”. You should have done it sooner, not started at the last possible minute.
  • Do go to class.
    • Students who don’t come to class will learn less. Yes, sure, there are some instructors who are *terrible*, I mean they couldn’t teach themselves out of a wet paper bag… but there are more good instructors, where you can actually learn something by attending class. The smaller the course, the more apparent the “no-shows” become. Hey, it’s your choice, just don’t ask the prof for an extension of anything.
  • Don’t ask an instructor to clarify things that are obvious.
    • If something is written in plain, easy to understand text, don’t ask an instructor to “clarify it”. If an assignment says to “include two 300 word articles”, don’t assume this means you can just copy them from somewhere. Assignments always imply original work. If an assignment says “Don’t do X.”, then don’t do X – don’t contact the prof asking if they can make an exception for you.
  • Do the work.
    • Assessments exist to assess a students knowledge. If a student performs poorly all the way through a semester, and gets a 46%, there is little anyone can to to push that to a passing grade. It is the students responsibility to do the work, and do it properly. If a substandard assignment is handed in, it will receive the grade it is due.

At the end of the day the experience you have at university is the one you make for yourself. Look university education is not perfect, far from it… it could do with a real shake up… and there are poor instructors. But on the flip-side, instructors are not your parents. If you don’t show up for class, and don’t read course material, then many instructors will put their energy towards those that are engaged. You can’t blame an instructor for your own inability to do well in a course. And there will be times when you have a bad instructor, and you will be required to rise to the challenge and teach yourself (we all had to do that at one time or another).

A limit to human abilities?

“Nobody is going to run 100 meters in five seconds, no matter how much is invested in training and machines. The same can be said about using the brain. The human mind is no different now from what it was five thousand years ago. And when it comes to mathematics, you must realize that this is the human mind at an extreme limit of its capacity.”

Herbert Robbins, Mathematician (1915-2001), on why there won’t be a quantum leap in mathematical ability (from Dijkstra, “On the cruelty of really teaching computer science“)

This is the way

Boba Fett got a bad rap in the original Star Wars trilogy. He was lambasted for being a bounty hunter, but what he was doing wasn’t necessarily bad, it was just what he did for a living. The Book of Boba Fett provides an interesting insight into Fett after the whole saga with the sarlacc. I thought the series was very good, albeit after the long, somewhat slow, build-up of the story-line, things were sorted far to quickly (which seems to be a common theme among shows these days). At least the final episode was somewhat realistic, and kudos to the Rancor, a species who also got a bad wrap in ROTJ.

I do find it funny that people knit-pick so much about every single frame of shows like The Book of Boba Fett… while I do enjoy watching some of them, particularly the ones based on plot analysis, the ones that go on and on about things they got wrong in the series do get annoying after a while. It’s meant to be a story, a fictitious story set in a fictitious galaxy (or possibly not, who really knows?), and as such it should be enjoyed for what it is – entertainment, and an escape from our own reality for a while. And in a world where every show in living memory is being remade, it’s nice to engage with new stories, even if we know some of the characters in them.

Star Wars provides us with a different perspective of the life in space. It makes you wonder if there are other civilizations out there somewhere. Possibly better, possibly worse, who knows? What we learn about the Star Wars universe it that the good/evil thing never was as cut and dry and we thought it was. Sure, Vader was bad, I mean really bad. But was he not a product of the circumstances in his life? Did he not suffer from some (serious) mental health issues? Very likely, no, extremely likely. But as bad as the Empire and Sith were, the Jedi were somewhat complacent, and too set in their old ways. Not willing to bend, even a little.

I hope there will be a second season of TBOBF, but maybe it makes more sense to move onto “books” on other notable SW characters? Maybe there will be a cross-over with Mando, leaving Tatooine in the good hands of Cobb Vanth? The thing is, with a universe so rich in characters, you almost have infinite material to work with. Honestly, the same cannot be said in the Trek universe, where although I am enjoyed Discovery Season 1, Picard seems to have spiralled into the old time-travel/Q/Borg garbage… like move on already… we are all over the time-travel/spatial anomaly thing (I mean S2 worked out okay, but you can’t keep playing the time-travel card).

McIlroy on C

“C is easy to write and (when well written) easy to read.”

McIlroy, M.D., Pinson, E.N., Tague, B.A., “Foreword”, The Bell Systems Technical Journal, 57(6), p.1899-1904 (1978)

On the complexity of C++

If you think C++ is not overly complicated, just what is a protected abstract virtual base pure virtual private destructor, and when was the last time you needed one?

− Tom Cargill, C++ Journal, Fall 1990