C question from Dan Garcia
Currently, I’m following Dan Garcia’s CS61C course (which is freely available online). On lesson 6, there’s an interesting puzzle (which, btw, I got it wrong – I’ll tell you why on the next post). The question is: which snippets prints always 5?
int *a-ptr; *a-ptr = 5; printf(“%d”, *a-ptr);
int *p, a = 5;
p = &a; ...
/* code; a & p NEVER on LHS of = */
ptr = (int *) malloc (sizeof(int));
*ptr = 5;
Option 2 says that there’s code between p = &a; and the printf line, but a and p are never on the left hand sign of an equals operation. With this data, which option (if any) do you think will always print 5?