how to use extern variable

Discussion in 'C++' started by Han, Nov 13, 2007.

  1. Han

    Han Guest

    If I define a variable in a.cpp as a global variable,but I want to use
    it in b.cpp. How to do that?
    For example
    //a.cpp
    vector<int> * p
    void main()
    {
    }

    //b.cpp How can I use "p" here?
    .......
     
    Han, Nov 13, 2007
    #1
    1. Advertising

  2. Han

    WaterWalk Guest

    On Nov 13, 5:31 pm, Han <> wrote:
    > If I define a variable in a.cpp as a global variable,but I want to use
    > it in b.cpp. How to do that?
    > For example
    > //a.cpp
    > vector<int> * p
    > void main()
    > {
    >
    > }
    >
    > //b.cpp How can I use "p" here?
    > ......


    Use "extern" to declare this variable before you use it. For example
    in b.cpp, you can write this:
    extern vector<int> *p;
    //use p...

    Generally, an extern declaration is put in a header file, so that it
    can be easily used by other files.
     
    WaterWalk, Nov 13, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Matthews
    Replies:
    5
    Views:
    2,547
    tom_usenet
    Aug 2, 2004
  2. Dan Elliott
    Replies:
    4
    Views:
    13,320
    Tom Widmer
    Dec 8, 2004
  3. smackdab

    extern access to variable

    smackdab, Dec 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    460
    Jack Klein
    Dec 8, 2003
  4. knight
    Replies:
    6
    Views:
    1,186
    Juha Nieminen
    Feb 17, 2011
  5. Andre
    Replies:
    5
    Views:
    564
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page