473,378 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to access the global

Hi,

int x = 10;
int main ()
{
int x = 5;
??? /* Access the global x here */
}

How to access the global x within main?

Nov 13 '05 #1
6 1383
"Ganesh Kundapur" <gk*******@yahoo.com> wrote in message
news:3F**************@yahoo.com...
Hi,

int x = 10;
int main ()
{
int x = 5;
??? /* Access the global x here */
}

How to access the global x within main?


As you have it, you cannot. But with a scope adjustment:

#include <stdio.h>

int x = 10;

int main(void)
{
{
int x = 5;
}

printf("%d\n", x); /* prints 10 */

return 0;
}

or a 'back door' access via a pointer:

#include <stdio.h>

int x = 10;

int main(void)
{
int *p = &x; /* must come *before* declaration of 'x' below */
int x = 5;

printf("%d\n", *p); /* prints 10 */
return 0;
}

Or more sensibly, change the name of one of the 'x's

Also, globals are generally not recommended without
a compelling reason to define them.

Is your question only academic, or is there a particular
problem you're trying to solve?

-Mike
Nov 13 '05 #2
dis
"Ganesh Kundapur" <gk*******@yahoo.com> wrote in message
news:3F**************@yahoo.com...
int x = 10;
int main ()
{
int x = 5;
??? /* Access the global x here */
}

How to access the global x within main?


It would be more sensible to rename one of both variables, but one
possibility would be:

int x = 10;
int main(void)
{
int x = 5;
{
extern int x;
x; /* the expression x will evaluate to 10 */
}
return 0;
}

Nov 13 '05 #3
Ganesh Kundapur wrote:

Hi,

int x = 10;
int main ()
{
int x = 5;
??? /* Access the global x here */
}

How to access the global x within main?


int x = 10;

int read_x (void) { return x; }
void write_x (int a) { x = a; }

int main (void)
{
int x = 5;

write_x(0);
return read_x();
}
Nov 13 '05 #4
Ganesh Kundapur <gk*******@yahoo.com> wrote:
int x = 10;
int main ()
{
int x = 5;
??? /* Access the global x here */
}

How to access the global x within main?


You can't. You've explicitly taken the global object out of scope within
main(). If you need it, don't do that.

Richard
Nov 13 '05 #5
Ganesh Kundapur wrote:
Hi,

int x = 10;
int main ()
{
int x = 5;
??? /* Access the global x here */
}

How to access the global x within main?


(a) Don't call the local in main `x` if you want to access a global
called `x`. [There are solutions that let you do this; they're
typically much more complciated and of less benefit, except for (b).]

(b) Don't call globals names like `x`, whether or not you want to
access them from inside functions with locals called `x`.

(c) Why are you asking?

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #6
In message <bl**********@news3.tilbu1.nb.home.nl>
"dis" <di*@hotmail.com> wrote:
It would be more sensible to rename one of both variables, but one
possibility would be:

int x = 10;
int main(void)
{
int x = 5;
{
extern int x;
x; /* the expression x will evaluate to 10 */
}
return 0;
}


That totally failed to occur to me (and all the other respondents I've seen).
I'm not sure whether to complement you on your C prowess, or be worried what
sort of programmer would have such a scoping construct at their fingertips.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nicola | last post by:
Hi Everyone, I am new to programming and would like to know how to open an access Report from within vb 6. I am trying to write a program to organise cross stitch threads. I have found out how...
0
by: Clifford Heath | last post by:
We've set up an SQL Server 2000 (build 8.0.761 - I think that's SP3) access control scenario like the one described in this article:...
3
by: amywolfie | last post by:
Hi all: I develop in both FileMaker Pro and Access. FileMaker has a global field type, whereby the value of the gfield is the same for all records. Is there an equivalent in MS Access...
3
by: Jorge Cecílio | last post by:
Hi! I would like to export some MS-Access reports output to pdf. However, the only possibility offered by Access (afaik) for me to export formatted output is snp (snapshot) (I use MS-Office...
10
by: Ronnie | last post by:
I've created a simple web application using VS2005 Beta 2. Basically, I've added a Web Form and a Global.asax file. In my Global.asax, I create a method like: public static void TestGlobal()...
11
by: DraguVaso | last post by:
Hi, I want to make a small application in VB.NET that relinks all the query's and tables in an Access database that are linked via ODBC to an SQL Server. It must be able to relink all the tables...
1
by: gm | last post by:
Hi; I have written a database that tracks all the installation we have ever done. I have a small heating company. I have recently started keeping a directory of digital photographs of the...
36
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are...
4
by: RgeeK | last post by:
I have a main module doStuff.py and another module utility.py. At the start of doStuff.py I call import utility.py Then I also proceed to initiallize some global variables sName = "" ...
6
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.