Why does this compile?

Started by
26 comments, last by GamerSg 20 years, 5 months ago
or, instead of using absolutely meaningless array index variables, why not?:
for(int wateverIndex=0; wateverIndex < watever; wateverIndex++){}for(int spellObjIndex=0; spellObjIndex < numOfSpellObjs; spellObjIndex++){    if( spellObjs[spellObjIndex].active )    {        spellObjs[spellObjIndex].updateAnimation();    }} 

it''s amazing how many logic errors good coding practice can help prevent.
Advertisement
quote:Original post by Anonymous Poster
or, instead of using absolutely meaningless array index variables, why not?:
for(int wateverIndex=0; wateverIndex < watever; wateverIndex++){}for(int spellObjIndex=0; spellObjIndex < numOfSpellObjs; spellObjIndex++){    if( spellObjs[spellObjIndex].active )    {        spellObjs[spellObjIndex].updateAnimation();    }}   


because noone can be bothered to type out a huge variable name when a single letter would suffice and everyone knows that 'i' is usually an array index so it doesn't really cause any confusion. and if the compiler was standards compliant (by default) then there wouldn't've been a problem.


[edited by - necromancer_df on October 21, 2003 6:36:33 PM]
quote:Original post by Anonymous Poster
Or instead of an ugly macro hack, just get into the habit of declaring variables outside of your for loop:

int i;
for(i=0; i{
}


That''s almost always the wrong thing to do. The macro, though strictly illegal, is by far the better solution.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
Indeed.
{ int i; for(i=0; i&lt;10; i++) {    // do whatever }} 

I enabled the option /Zc:ForScope in visual net that forces the scope of for loop, then recompiled.

The result has been interesting: the compiler returned some errors where I was using the i and j variables in functions where they were not defined at all.

if you want to enable that compiler option on properties, it is under "C/C++" , then "Language"

I''m curretly "looping" all my projects and enabling that option.


Btw, is there any thread here that discuss what the MSVC compiler options do, and which could be the best configuration of options?
The scoping block symbols in C/C++ are the { } and thus anything declared outside of it belongs in the next higher scope - block scope, function scope or file scope. For correct implementations that follow compiler and standards the variable should be declared previous to its use. The for (int i... variable declaration is an unsafe habit to use, due to differing compiler implementations.

Just because the macro one looks nice, doesnt mean its safe. And certainly by implementing the macro any programmer (or even when you come back to your code much later) may not know the macro has been defined and could lead to problems later. For instance it is safe to do various operations in the 'for' statement, if you apply the macro you have changed the scope of those operations in the 'for' statement. Maybe not a massive deal, its more about being careful and making code understandable and useable. It comes down to expected behaviour, thats really what standards are for anyway.


[edited by - Grover on October 22, 2003 4:19:40 AM]
In what way does the macro provide anything other than the correct semantics for for statements?
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

This topic is closed to new replies.

Advertisement