简体   繁体   中英

post-increment and assignment in an array index

I have encountered a terse piece of code which I am finding very difficult to decipher.

for (int k = 0; k < count; k++)
{
    C[p=w[T[k]]++] = Y1[k];
    CC[p] = Y2[k]
}

w and T look like this:

w = [0 3 6 8]
T = [2 0 3 2 1 0 1 3 0 1]

I am unable to figure what is the index that the array C and CC are going to get when k = 0 and a way to read the code easily.

This is what I had tried to do.

When

k = 0,
T[0] = 2,
w[2] = 6
p = 6
w[2] = 7
c[6] and cc[6] are assigned

Rewrite it. This code is equivalent to:

for (int k = 0; k < count; k++)
{
    p = w[T[k]]++;
    C[p] = Y1[k];
    CC[p] = Y2[k]
}

which is the same as

for (int k = 0; k < count; k++)
{
    p = w[T[k]];   // For k=0 => w[2]=6 => p=6
    w[T[k]]++;     // For k=0 => increment w[2], so w[2] is 7
    C[p] = Y1[k];  // C[6] = Y1[0]
    CC[p] = Y2[k]  // CC[6] = Y2[0]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM