Q:

Char array in c & pointer addition

My instructors give me the following function. So don't blame me for ambiguity lol
void step_step_step(char *first, char *second, char *third)
{
    if (third[3] == second[2] + 8 && second[2] == first[1] + 8)
        printf("8: Illinois\n");
    else
                printf("8: ERROR\n");
}

I try to call the function in the this way:
char *p8_1 = (char*) malloc(sizeof(char)*11);
    char *p8_2 = (char*) malloc(sizeof(char)*11);
    char *p8_3 = (char*) malloc(sizeof(char)*11);
    p8_1[9] = 'u';
    p8_2[2] = p8_1[9];
    p8_2[10] = p8_1[9];
    p8_3[3] = p8_2[10];
    step_step_step(p8_1, p8_2, p8_3);

And it keeps printing error. What am I doing wrong here? I don't understand why that doesn't work when my other solution does:
p8_2[2] = p8_1[1] + 8;
p8_3[3] = p8_2[2] + 8;
step_step_step(p8_1, p8_2, p8_3);

A:

Possible operator precedence bug:
Change to:
if ((third[3] == second[2] + 8) && (second[2] == first[1] + 8))
The wiki article on 'Order of operations' tells more about the evaluation precedence.
If you can't explain to someone in the blink of an eye, in which order some expression will be evaluated - you should use enough parentheses that it won't matter (because no order precedence to care of) or that it's instantly obvious how the clauses are separated.
EDIT:
A closer look makes me realize something fishy is going on. You're not incrementing any pointers. If you were, you would do something like if ((third[3] == second[2 + 8]) && (second[2] == first[1 + 8]))

A:

You believe the if condition should pass, since all the elements you are intending to check is initialized to 'u'. In your code, this is true for p8_3[3], p8_2[10], p8_2[2], and p8_1[9]. Then, there is the call:
step_step_step(p8_1, p8_2, p8_3);

As third takes the p8_3 value, indexing its 3rd element is fine, and it has value 'u'. However, second[2] has the value 'u', and then 8 is added to it before the == comparison. This results in the == to result in a false evaluation. The && is short-circuited, and the else case of the if is taken.
If short-circuiting had not taken place, the right side expression of the && would compare second[2] with first[1] + 8. As first[1] is uninitialized, this would have resulted in undefined behavior.

The indexing operator ptr[index] behaves like *(ptr + index). So, the expression:
second[2] + 8

translates into:
*(second + 2) + 8

which is definitely NOT the same as *(second + 2 + 8).

