Click to See Complete Forum and Search --> : Has this syntax for accessing an array EVER been legal?
Colson33
April 29th, 2003, 09:42 AM
k, a friend of mine swears that in a class an instructor told him that this was legal. Now this class was years ago (he's a 35 year old programmer/manager), so what I'm wondering if maybe in years gone past or in some early phase of C was this syntax EVER legal.
int bla[10];
[3]bla = 1;
Obviously the brackets are being placed before the variable, I said it hasn't, a few friends also have said the same thing, but he wants more proof. So any of you guys out there (and hopefully some oldies) can give me some proof that this have never been legal I would appreciate it.
Thanks,
Chris Olson
Paul McKenzie
April 29th, 2003, 09:58 AM
Originally posted by Colson33
k, a friend of mine swears that in a class an instructor told him that this was legal. Now this class was years ago (he's a 35 year old programmer/manager), so what I'm wondering if maybe in years gone past or in some early phase of C was this syntax EVER legal.
int bla[10];
[3]bla = 1;
Obviously the brackets are being placed before the variable, I said it hasn't, a few friends also have said the same thing, but he wants more proof. So any of you guys out there (and hopefully some oldies) can give me some proof that this have never been legal I would appreciate it.
Thanks,
Chris Olson Yes, it's legal, but only used in obsfuscation contests.
The reason is that bla[10] is equivalent to *(bla + 10). [10]bla is equivalent to *(10 + bla). Same thing.
Regards,
Paul Mckenzie
Graham
April 29th, 2003, 10:02 AM
See section 5.2 of the C++ standard. I don't know about C standards, but the definition of a postfix expression hasn't exactly changed. My copy of K&R has long since disappeared, but I'm pretty confident that you won't find that syntax in there, either.
Graham
April 29th, 2003, 10:06 AM
Paul, you surprise me. I've never seen that and I don't quite see how it could work the way you describe. The standard is quite clear:
postfix-expression:
primary-expression
postfix-expression [ expression ]
I don't see how that can be worked around to
[ expression ] (postfix)-expression.
Paul McKenzie
April 29th, 2003, 10:14 AM
Originally posted by Graham
Paul, you surprise me. I've never seen that and I don't quite see how it could work the way you describe. The standard is quite clear:
postfix-expression:
primary-expression
postfix-expression [ expression ]
I don't see how that can be worked around to
[ expression ] (postfix)-expression. Maybe you're right. But I know I have seen this type of code before using arrays and pointers where you would say "no way", but it works.
I will do a google search for "C obfuscation" and see what turns up. Right now, I can't seem to remember exactly what the nutty syntax looked like.
Regards,
Paul McKenzie
Paul McKenzie
April 29th, 2003, 10:18 AM
OK. Here it is:
int main()
{
int blah[3];
blah [0] = 1;
1 [blah] = 2;
}
Regards,
Paul McKenzie
Graham
April 29th, 2003, 11:57 AM
That makes more sense, since "1" is a literal, which is a valid primary expression, and hence a valid postfix-expression (although it is an r-value).
But I still dont believe the format with the brackets at the front.
And I would fire anyone who actually put it in production code.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.