Advertisement

CppArray[7,8]

Started by March 14, 2002 06:06 PM
9 comments, last by matrix2113 22 years, 9 months ago
Why would creating an array like array[1,2] work? I''m know that it would only create one dimension, but the only use I know for the comma is in functions. Since no ones uses commas when defineing arrays (to my knowledge), why doesn''t this produce an error? "I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green" -Peter Griffin
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that really matters is green"-Peter Griffin
The comma operator causes the expressions on either side of it to be evaluated, lhs first, and then returns the rhs result. Thus, doing int array[1,2]; is equivalent to doing int array[2];

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
Advertisement
The comma operator returns the value of the last expression in the sequence. a = (b,c,d) where b, c and d are expressions is equivalent to a = d (excepti if b and c have side-effects, of course)

Therefore array[1,2] is equivalent to array[2]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Why would they allow that though? It creates no extra functionality; its pointless and error-prone

"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that really matters is green"-Peter Griffin
Maybe, but it lets you do for( i = 0, j = 1; i < 10; ++i, j*=2 ); The 3 parameters of a for loop are expressions, not statements.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote: Original post by matrix2113
Why would they allow that though? It creates no extra functionality; its pointless and error-prone

Don''t be silly. Array dimension declarations simply require an expression that evaluates to a constant integral value (which lets you use consts and #defines to modify compile-time behavior), while array indexing requires the expression to evaluate to an integral value (which allows you to use the return values of functions, etc as indices). Your problem is that you think the language is designed/implemented on a per-instance basis ("okay, we allow this for arrays, this for pointers...") rather than in terms of general/generic syntax.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
Advertisement
quote: Original post by Fruny
Maybe, but it lets you do for( i = 0, j = 1; i < 10; ++i, j*=2 ); The 3 parameters of a for loop are expressions , not statements.



In C++ that is true, but in C the 2nd parameter is a statement and not an expression.


- Kevin "BaShildy" King
Game Programmer: DigiPen
www.mpogd.com
- Kevin "BaShildy" KingGame Programmer: DigiPenwww.mpogd.com
quote: Original post by BaShildy
In C++ that is true, but in C the 2nd parameter is a statement and not an expression.


K&R 2nd ed, section 3.5, p60

for( expr1; expr2; expr3) statement
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote: Original post by Fruny
K&R 2nd ed, section 3.5, p60

for( expr1; expr2; expr3) statement


I stand corrected. I could''ve sworn that C''s for and C++''s for had a different definition of the 2nd parameter. Was it changed as of C99 or has it always been that way?



- Kevin "BaShildy" King
Game Programmer: DigiPen
www.mpogd.com
- Kevin "BaShildy" KingGame Programmer: DigiPenwww.mpogd.com
K&R 2nd ed definitely predates C99. And I vaguely remember it being that way from the first time I read K&R, about ''95.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

This topic is closed to new replies.

Advertisement