NO

Author Topic: view source code during debugging  (Read 23029 times)

CommonTater

  • Guest
Re: view source code during debugging
« Reply #30 on: May 28, 2012, 04:23:18 PM »
@ Vedro...
 
What I don't think you're undestanding is the rules of scope in C...
Many beginners do not appreciate that the { and } characters are an active part of their code, viewing them only as punctuation marks... which they are not.
 
Here's a bit of code to demontrate the concept... (I've also attached the project so you can run and test it...)
 
Code: [Select]

 // demonstration of rules of scope
 
int main (void)
  {                // beginning of outer scope
    int x;
    int y;
    x = 10;
    y = 20;
    printf("x = %d, y = %d\n",x,y);
 
    {               // beginning of inner scope
      int x;
      x = 10233;
      printf("x = %d, y = %d\n",x,y);
    }               // end of inner scope
 
    printf("x = %d, y = %d\n",x,y);
       
    return 0;
  }                  // end of outer scope
 
The output from this is...
x = 10, y = 20
x = 10233, y = 20
x = 10, y = 20
 
So how can we change x in the inner scope but it is not changed in the outer scope? 
How can y be visible in the inner scope?
 
Because, for all intents and purposes each scope is it's own little program... demarked by the braces... braces begin and end scopes, they're not merely punctuation marks. Values from larger scopes penetrate into smaller scopes... values from smaller scopes do not carry over to larger scopes... 
 
Thus when you try to return your array from the "inner scope" of a function call, you are breaking the rules of scope and it will fail.
 
These rules have to be understood if you are to write useable programs...
 
 
 
 

vedro-compota

  • Guest
Re: view source code during debugging
« Reply #31 on: May 28, 2012, 06:46:31 PM »
thank you! I'll study this example.