tags:

views:

208

answers:

9

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++)
{
cout<<a[b][c];
}
+2  A: 

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.

Georg Fritzsche
+7  A: 

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.

Anon.
I think this is the first time I've ever seen anyone say WHY a[b] and b[a] are equivalent... it seems blaringly obvious now that it's pointed out, but seeing it written as *(a+b) made that syntax finally clear after years of C coding.
Tanzelax
Michael Burr
@Anon: there's one time when I use the `index[array]` form - in a macro for getting the number of elements in an array. Using the `index[array]` form prevents the expression from working accidentally for a C++ class that overloads the '`[]`' operator (admittedly, it's a very specialized use).
Michael Burr
That helps e.g. in array-size macros, see e.g. this answer: http://stackoverflow.com/questions/1598773/is-there-a-standard-function-in-c-that-would-return-the-length-of-an-array/1598827#1598827
Georg Fritzsche
@Michael: Yeah, I've seen it separately that a[b] = *(a+b) (as you said, it's in just about every C text), and that a[b] = b[a], but for some reason the two bits just never connected.
Tanzelax
@Tanzelax: I see - I'd agree that those dots aren't always connected explicitly, and that the connection might not be something that jumps out.
Michael Burr
@Michael: Funny, i only now noticed that the linked answer was from you - otherwise i would have mentioned that.
Georg Fritzsche
I see them more as syntactic wasabi. Use them without caution, and...
Daniel Daranas
A: 

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]];
}
Kugel
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}]

TP
A: 

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.

sepp2k
+2  A: 
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]]
fbrereto
Same approach I was using, but you got their first and better...
dmckee
It's not c[0..4], it should be c[b[0..4]]
Reed Copsey
@Reed: Updated, thanks.
fbrereto
A: 

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.

Reed Copsey
+5  A: 

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.

John Knoeller
I see. I didn't know 3[b] was the same as b[3]... okay I get how it works now. Thanks a lot all.
in other words, array subscripting is commutative in C. Note that this is not the case in all variants of C++ (Visual C++, for instance).
Demi
A: 

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?"

Notinlist
A's not used until the for loop, in which case it gets initialized to 0...
Reed Copsey
My bad. I used to declare and initialize loop variables inplace or as late as it is possible so it missed my mind. :-)
Notinlist