Solved

How to declare a global variable inside a function?

Posted on 2008-09-30
8
2,343 Views
Last Modified: 2013-11-17
How do I declare a global variable inside a function? (so that it can be used by other functions)
0
Comment
Question by:Chrysaor
[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
8 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 22605830
Hi Chrysaor,

that's not possible (at least not in C/C++). You have to declare that variable at the global (or a namespace) scope.

ZOPPO
0
 
LVL 10

Expert Comment

by:peetm
ID: 22605846
>>How do I declare a global variable inside a function? (so that it can be used by other functions)

Only by having the function return a reference or pointer to the variable.
0
 
LVL 14

Expert Comment

by:wayside
ID: 22605932
Do you really need it to be global, or just be able to use it in other functions?

You can't define a truly global variable a function, although by declaring it as static you can make it persist past the life of the function. Then you can return a pointer or reference to it as peetm suggested.

int *my_func() {

  static int foo;
  .
  .
  .

  return &foo;
}

Because foo is declared as static, it lives on after the function returns.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:Zoppo
ID: 22605982
You could even do something like this to restrict usage of such a global variable to a given set of functions, i.e.:

> class A
> {
>      static bool Flag; // declare the global flag
>      friend void foo(); // allow 'foo' to access the flag
>};

>bool A::Flag = true; // instantiate the flag

void foo()
{
      A::Flag = true; // OK
}

void bar()
{
      A::Flag = true; // Error: no access to private member
}

ZOPPO
0
 

Author Comment

by:Chrysaor
ID: 22606092
Basically, I have a function which it returns an int (let's say int number(); ) I have another one function, let's call it void body(); . When the body() is called, it creates a vector like this: vector<int> P(number()), and assign values to the vectors e.g P[0] =1; P[1] = 7; etc etc. Then , I want from another function to recall the values of e.g P[0],P[1] etc..

That's why I asked if it was possible to declare vector<int> P(number) a global variable. Any suggestions on how to solve my problem?
Thanks a lot, sorry for your time.
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 22606162
Well, you can simply return the vector from body(), i.e.:

> std::vector <int> body()
> {
>  std::vector <int> P( number() );
>  // initialize P ...
>  return P;
> }
> ...
> std::vector <int> P = body();

or pass it by reference

> void body( std::vector <int> &P )
> {
>  P.resize( number() );
>  // initialize P ...
> }
> ...
> std::vector <int> P;
> body( P );

ZOPPO
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 22606175
Addition: the second possibility is more efficient since there's no need to create a copy of the vector ...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 23246887
>> the second possibility is more efficient since there's no need to create a copy of the vector
Modern compilers support Named Return Value Optimization (NRVO), so 1 and 2 are likely to be optimal solutions.
http://msdn.microsoft.com/en-us/library/ms364057.aspx
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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 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