Priority arr [x ++] -

I am confused about how to parse the priority of operations in C. line

countArray[*string++]--

      

Performs as I want, but I don't understand the steps that lead to countArray [* string] - being evaluated to string ++.

My research on priority and binding to C provided no answers related to this case, and I'm wondering about the general rules for post / pre-increment and post / pre-decrement when combined with other posts / increments.

How does C know to evaluate it this way?

void discountChars(char* string, char** countArray)
{
    int test;
    while(*string) {
        test = *string;

        //why is countArray[*string]-- evaluated before string++ is incremented?
        countArray[*string++]--;
        printf("countArray[%d] = %d\n", test, countArray[test]);    
    }
}

      

+3


source to share


3 answers


You can break this:

    countArray[*string++]--;

      

down:



    char index = *string;   // get char from `string` into temporary index
    string++;               // increment `string`
    countArray[index]--;    // decrement `countArray` value at given index

      

and then it should be clearer what's going on.

+9


source


As has been said many times, priority is not related to the order of evaluation. The only thing in C that can affect the order of evaluation is consistency. Priority has nothing to do with it.

It is also unclear where you got the weird idea of ​​what is " countArray[*string]--

evaluated before *string++

". This is simply not possible. The expression in []

will always be evaluated first, since its result is required to perform access to the item (i.e., sequenced before accessing the item). This means that the opposite is true: it *string++

evaluates to countArray[*string]--

.

So the sequence of steps is here

  • Please rate *string++

    . The result of this expression is the original value *string

    . Let's designate it tmp

    .

    This expression also "assigns" a side effect - the increment string

    . But this increment shouldn't happen right now.

  • Please rate countArray[tmp]--

    . The result of this expression is the original value countArray[tmp]

    . This result is immediately canceled.

    This expression also "plans" a side effect - decrement countArray[tmp]

    . But this decrement shouldn't happen right now.

  • Complete the evaluation of the complete expression. If any of the above side effects are still pending, complete them now.

For example, one possible scoring schedule might look like this



char tmp = *string; // subexpression inside `[]`
countArray[tmp]; // full expression, result discarded
countArray[tmp] = countArray[tmp] - 1; // side-effect
string = string + 1; // side-effect

      

Another possible evaluation schedule is

char tmp = *string; // subexpression inside `[]`
string = string + 1; // side-effect
countArray[tmp]; // full expression, result discarded
countArray[tmp] = countArray[tmp] - 1; // side-effect

      

It can even be evaluated as

string = string + 1; // side-effect
char tmp = *(string - 1); // subexpression inside `[]`
countArray[tmp]; // full expression, result discarded
countArray[tmp] = countArray[tmp] - 1; // side-effect

      

+7


source


The precedence controls the grouping of operators and operands, not the order of evaluation.

An expression *string++

must be evaluated before being used as an array index; however, the side effect of updating string

can occur after evaluating a larger expression. The following sequence of events is possible:

t1 <- *string
countArray[t1] <- countArray[t1] - 1
string <- string + 1

      

Then again like this:

t1 <- *string
string <- string + 1
countArray[t1] <- countArray[t1] - 1

      

+1


source







All Articles