开发者

C C++ array.... need help understanding code

Can you please explain this code? It seems a little confusing to me Is "a" a double array? I would think it's just an integer, but then in the cout statement it's used as a double array. Also in the for loop condition it says a<3[b]/3-3, it makes no sense to me, however the code compiles and runs. i'm just having trouble understanding it, it seems syntactically incorrect to me

int a,b[]={3,6,5,24};
char c[]="This code is really easy?";
for(a=0;a<3[b]/3-3;a++)
{
开发者_StackOverflow社区cout<<a[b][c];
}


Array accessors are almost syntactic sugar for pointer arithmetic. a[b] is equivalent to b[a] is equivalent to *(a+b).

That said, using index[array] rather than array[index] is utterly horrible and you should never use it.


Wow. This is really funky. This isn't really 2 dimensional array. it works because c is an array and there is an identity in the C language that treats this

b[3]

as the same as this

3[b]

so this code translates into a loop that increments a while a < (24/3-3) since 3[b] is the same as b[3] and b[3] is 24. Then it uses a[b] (which is the same as b[a]) as an index into the array c.

so, un-obfuscated this code is

int a;
int b[] = {3,5,6,24}
char c[] = "This code is really easy?";
for (a = 0; a < 5; a++)
{
    cout << c[b[a]];
}

which is broken since b[4] doesn't exist, so the output should be the 3rd, 5th, 6th and 24th characters of the string c or

sco?

followed by some random character or a crash.


No, two variables are declared in the first statement: int a and int b[].

a[b][c] is just a tricky way of saying c[b[a]], that is because of the syntax for arrays: b[0] and 0[b] are equivalent.


int a,b[]={3,6,5,24};

Declares two variables, an int a and an array of ints b

char c[]="This code is really easy?";

Declares an array of char with the given string

for(a=0;a<3[b]/3-3;a++)

Iterates a through the range [0..4]:

  • 3[b] is another way of saying b[3], which is 24.
  • 24 / 3 = 8
  • 8 - 3 = 5

cout << a[b][c];

This outputs the following result:

  • a[b] is equivalent to b[a], which will be b[0..4]
  • b[0..4][c] is another way of saying c[b[0..4]]


Well there is a simple trick in the code. a[3] is exactly the same as 3[a] for c compiler.

After knowing this your code can be transformed into more meaningful:

int a,b[]={3,6,5,24};
char c[]="This code is really easy?";
for(a=0;a<b[3]/3-3;a++)
{
cout<<c[b[a]];
}


a<3[b]/3-3 is the same as writing

a < b[3]/3-3

and a[b] is the same is b[a] since a is an integer sp b[a] is one of the items from {3,6,5,24}

which then means a[b][c] is b[a][c] which is either c[{3,6,5,24}]


foo[bar] "expands" to "*(foo + bar)" in C. So a[b] is actually the same as b[a] (because addition is commutative), meaning the ath element of the array b. And a[b][c] is the same as c[b[a]] i.e. the ith char in c where i is the ath element in b.


Okay - first, let's tackle the for loop.

When you write b[3], this is equivelent to *(b+3). *(b+3) is also equivelent to *(3+b), which can be written as 3[b]. This basically can be rewritten, more understandably, as:

for(a=0; a < ((b[3]/3) - 3); a++) 

Since b[3] is a constant value (24), you can see this as:

for(a=0; a < ((24/3) - 3); a++) 

or

for(a=0; a < (8 - 3); a++) 

and finally:

for(a=0; a < 5; a++) 

In your case, this will make a iterate from 0-4. You then output a[b][c], which can be rewritten as c[b[a]].

However, I don't see how this compiles and runs correctly, since it's accessing c[b[4]] - and b only has 4 elements. This, as written, is buggy.


First: 'a' is not initialized. Let's assume that it is initialized to 0.

'3[b]/3-3' equals 5. The loop will go from 0 to 4 using 'a'. ('3[b]' is 'b[3]')

In the a==4 step 'a[b]' (so 'b[a]') will be out of bounds (bounds of 'b' is 0..3) so it has undefined behavior. On my computer somethimes 'Segmentation fault' sometimes not. Until that point it outputs: "soc?"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜