# Fun with Pointers (or maybe Fun with Engineering Students)– September 20, 2016

I was a teaching assistant for 18-349, Introduction to Embedded Engineering for three semesters in college and I really enjoyed it.  One semester, the professor asked each of the TAs to create a test problem, and I had quite a lot of fun with that, though my students didn't.  Only two students out of 100+ got it entirely correct, though maybe 30% got most of the problem correct.  Students always have trouble with pointers, and understanding how their are addressed, de-referenced, etc.

Here is the problem (note that you are at a disadvantage compared to the students because they should have known what size integers and integer pointers are):

Playing with pointers with ARM 7

Given:

int str = {0,1,2,3,4,5,6,7,8,9};

int i, j, tmp;

int *K, *L;

For the code examples of A, B, C, and D below, mark which samples correctly reverse the order of "str".  If not correct, explain why for each such sample.  Assume 32-bit words.

A. for (i=0, j=9; i<5; i++, j--){

tmp = str[i];

str[i] = str[j];

str[j] = tmp;

}

B. for (i=str,L=i+36; i<L; i=(int*)i+1; L--){

tmp = *(int*)i;

*(int*)i = *L;

*L = tmp;

}

C. for (K=&str; L = &str; L-K+1 != str; K++, L--){

tmp = *(K-1);

*(K-1) = *(L-1);

*(L-1) = tmp;

}

D. for (K=&str, L=K+9; K<L; K++,L--){

tmp = *K;

*K = **(&L);

*L = tmp;

}

The reason for the test problem is that I saw students writing things like *&var and if it didn't compile, add another * or & until it did, but without any understanding of what those symbols actually meant.  Though almost everyone got it wrong, some students did take the time afterwards to go through the code and actually understand what was happening.  It is my belief that if you can understand these code samples, you understand pointers completely.

Some students objected to the answer on moral or philosophical grounds, and I think the professor didn't end up weighting this question all that much when he scaled the grades.

One student went to office hours with the professor and managed to convince the professor that I was wrong, but I met with the student and we typed it into a compiler and ran it so he could see how it worked.

Note that I'm not saying this is the right way to write code.  Also, note that despite this test problem, students always rated me really high on the evaluations...