User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,944 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,902 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1615 | Replies: 8
Reply
Join Date: Aug 2005
Posts: 595
Reputation: SpS is on a distinguished road 
Rep Power: 4
Solved Threads: 30
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Scope Problem

  #1  
Sep 13th, 2005
Plz look at this code

#include<iostream.h>

int x=0;

int main()
{
int x=1;
{
   int x=2;
   cout<<::x;
   
cout<<x;
}
return 0;
}
<< moderator edit: added [code][/code] tags >>

does anybody know how can i access main local variable x, from inside the block, without using pointers or references?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: Oklahoma
Posts: 902
Reputation: chrisbliss18 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 21
chrisbliss18's Avatar
chrisbliss18 chrisbliss18 is offline Offline
Posting Shark

Re: Scope Problem

  #2  
Sep 13th, 2005
You could always just use better variable naming conventions. Using "x" for the name of every variable doesn't help you, and it doesn't help anybody looking at your code.
Did we help you? Did we miss the point entirely? Update your thread and let us know.
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
Reply With Quote  
Join Date: Aug 2005
Posts: 595
Reputation: SpS is on a distinguished road 
Rep Power: 4
Solved Threads: 30
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Scope Problem

  #3  
Sep 13th, 2005
Well...this was the question asked to me in my viva exam...and i think having same name in different scopes makes the question more tricky
Reply With Quote  
Join Date: Feb 2005
Location: Ballarat, Australia
Posts: 164
Reputation: Paul.Esson is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 6
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: Scope Problem

  #4  
Sep 17th, 2005
Your problem contains the ans.

cout<<::x;
Reply With Quote  
Join Date: Aug 2005
Posts: 595
Reputation: SpS is on a distinguished road 
Rep Power: 4
Solved Threads: 30
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Scope Problem

  #5  
Sep 18th, 2005
I guess u didn't understood what i was asking.....i am not talking about the global variable ....iam talking about the local variable inside the main....but thanx anyways i was able to solve this problem
Reply With Quote  
Join Date: Oct 2004
Location: India
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

Re: Scope Problem

  #6  
Sep 25th, 2005
What's the solution? I mean how do you access the variable inside the main from the inner block?
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
Reply With Quote  
Join Date: Jul 2005
Location: London
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: Scope Problem

  #7  
Sep 25th, 2005
you cant. watch.

int x=0; // global x
int main()
{
   int x=1; // main x
   {
       int x=2; // block x
       cout<< x<<endl; // prints block x
       cout<< ::x <<endl; // prints global x accessed with :: scope resolution operator
      // there is no way to access main x from here
   } // block x dies here
   cout<< x; // prints main x
   cout <<::x; // prints global x
   return 0;
}
The basic rule is a name in an inner scope hides the name in all outer scopes unless the scope resolution operator can be used to access it which is not possible with function scope local variables.
Reply With Quote  
Join Date: Aug 2005
Posts: 595
Reputation: SpS is on a distinguished road 
Rep Power: 4
Solved Threads: 30
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Scope Problem

  #8  
Sep 25th, 2005
Originally Posted by Stoned_coder
you cant. watch.

Yes you can

Here is the solution
#include<iostream.h>

int x=0;

int main()
{
int x=1;
{
   cout<<x;//access it before declaring variable in this local scope
   int x=2;
   cout<<::x;
   cout<<x   

}
return 0;
}
Reply With Quote  
Join Date: Sep 2004
Posts: 6,019
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

Re: Scope Problem

  #9  
Sep 25th, 2005
>Yes you can
Once you realize that it's a stupid trick question, of course.
Member of: Beautiful Code Club.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:54 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC