0

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);
2
  • 3
    Check the precedence of + and && Commented Sep 6, 2013 at 20:12
  • 1
    Your + operators are adding characters, not pointers, so there's no "pointer addition" here. Instead you're comparing 'u' to 'u' + 8 and getting non-equal (not suprisingly) Commented Sep 6, 2013 at 20:37

3 Answers 3

5

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]))

Sign up to request clarification or add additional context in comments.

1 Comment

There is no operator precedence issue per se, although parentheses may add clarity.
3

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).

8 Comments

How can you even figure out what he's trying to do? :) I'm trying really hard and failing.
this ones not clear - why wouldnt he say second[10]; maybe he means 'the ascii char 8 steps higher'; although his test doesnt set it up that way
I am inferring the intended outcome by how he initialized the input.
Again, my instructors wrote the function. I took it as the the third char arrays index [3] should hold the same character as the second char arrays index [10].. since it's [2] + 8. And the second char arrays index [2] should store the same character as the first char arrays index [9]. since it's [1] + 8
The [] operator does not distribute over +.
|
1

Looks like p8_1[1] has not been initialized. In your "working" example you assign the random value that's in p8_1[1] to p8_2[2], resulting in their equality. If you're trying to do pointer addition, you need to use a different syntax:

(pointer + (8*sizeof(char))[<index>]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.