|
-
April 29th, 2003, 09:42 AM
#1
Has this syntax for accessing an array EVER been legal?
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
-
April 29th, 2003, 09:58 AM
#2
Re: Has this syntax for accessing an array EVER been legal?
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
-
April 29th, 2003, 10:02 AM
#3
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.
Last edited by Graham; April 29th, 2003 at 10:06 AM.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
April 29th, 2003, 10:06 AM
#4
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
April 29th, 2003, 10:14 AM
#5
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
Last edited by Paul McKenzie; April 29th, 2003 at 10:17 AM.
-
April 29th, 2003, 10:18 AM
#6
OK. Here it is:
Code:
int main()
{
int blah[3];
blah [0] = 1;
1 [blah] = 2;
}
Regards,
Paul McKenzie
-
April 29th, 2003, 11:57 AM
#7
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|