/* I want to write a program that returns the last element of a linked list of elements. For example, last(2,4,6,8) should return 8. But I'm going to make an intensional mistake: */ #include #include typedef long long int64; // 64 bit signed integers typedef struct cell* list; struct cell { int64 car; list cdr; }; list cons(int64 head, struct cell* tail) { struct cell* A = (struct cell*)malloc(sizeof(struct cell)); A->car = head; A->cdr = tail; return A; } int64 lastval(struct cell * M) // written with a mistake: { while (M->cdr != NULL) M = M->cdr; return (int64)M; // what should this really be? } int main() { struct cell* M = cons(2,cons(4,cons(6,NULL))); printf("%d\n", lastval(M) + 4); // give me 10 PLEASE! } /* I used 64 bit ints because newer C compilers use 64 bit memory addresses (pointers) to illustrate the problem. Without the type cast (int64)M, the compiler will either give an error or warning, and some compilers will even try to apply the type cast automatically. With the type cast the program compiles AND RUNS without any error messages, but it is still clearly wrong. */