Solved

Creating and initializing public variable

Posted on 2006-11-22
5
229 Views
Last Modified: 2010-04-01
Hello!

Assume I have a code like this:

static void main() {

}

int test() {

}

I want to define a variable in this code which can be accessible from main and int.

Also if I define a variable for example X in main and another X in test, how can I call test's X from main?

Thanks
0
Comment
Question by:CSecurity
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Assisted Solution

by:xRalf
xRalf earned 250 total points
ID: 17994202
Hi,

define the variable as global before main
e.g.

int x;
static void main() {

}

int test() {

}

If you define the variable X in test() it will be local variable for test() and it is impossible to call it from main. It's allocated on the stack and exists only during the function call.
0
 
LVL 17

Author Comment

by:CSecurity
ID: 17994206
But I heard something like test::X or something like this
0
 
LVL 17

Author Comment

by:CSecurity
ID: 17994252
is it possible?
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17994295
>> is it possible?
No it is not possible
0
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 250 total points
ID: 17994307
>> But I heard something like test::X or something like this
This is possible with static class members.
Or, namespace...

Example:

namespace test {
      int X;
};

static void main() {

      test::X = 0;
}
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 138
C++ mouse_event mouse look 7 111
Indy 10 not Receiving UDP broadcast 3 43
Coding for the first time 9 62
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question