Sharing static variables or function between source files.

Discussion in 'C Programming' started by shantanu, Nov 28, 2006.

  1. shantanu

    shantanu Guest

    Hi,
    How can we declare a static variable or function declared in one
    source file, which is to be used in other source file?
    shantanu, Nov 28, 2006
    #1
    1. Advertising

  2. shantanu <> wrote:

    > How can we declare a static variable or function declared in one
    > source file, which is to be used in other source file?


    1) Don't declare them to be static.

    2) Add extern declarations to every source file except the one where you
    define the function:

    A.c

    void foo() {
    /* ... */
    }

    B.c

    extern void foo;

    void bar() {
    /* ... */
    foo();
    }

    How to correctly invoke your linker to make sure this works is a topic
    for a different newsgroup.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Nov 28, 2006
    #2
    1. Advertising

  3. In article <>,
    shantanu <> wrote:
    > How can we declare a static variable or function declared in one
    >source file, which is to be used in other source file?


    You don't. If you need to use it in another source file, do not
    declare it as static, or else find a way to get a pointer to it.

    For example, you could add a routine which was

    SomeType MyStaticVariable;

    SomeType *get_pointer_to_my_static_variable(void) {
    return &MyStaticVariable;
    }

    Then to use the space, get_pointer_to_my_static_variable() and
    use the pointer returned by that.
    --
    All is vanity. -- Ecclesiastes
    Walter Roberson, Nov 28, 2006
    #3
  4. shantanu

    Jack Klein Guest

    On 27 Nov 2006 19:11:25 -0800, "shantanu" <>
    wrote in comp.lang.c:

    > Hi,
    > How can we declare a static variable or function declared in one
    > source file, which is to be used in other source file?


    If you want to easily share it with other translation units, define it
    without the static keyword.

    Whether that answers your question depends on what you mean by static,
    since the keyword has several different meanings in C. All objects
    defined at file scope have static storage duration. Adding the static
    keyword only changes their linkage from external to internal.

    If you mean objects defined at file scope with the static keyword, or
    functions defined with the static keyword, the only way to access or
    call them from other translation units is via a pointer initialized in
    or provided by the translation unit that contains the definition.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 28, 2006
    #4
  5. shantanu

    Miles Davis Guest

    You can use static var but share a pointer ( which is often a readonly
    one)

    For example:
    a.c:
    static int a = 0;
    const int * p = &a;

    b.c:
    extern const int * p;

    I use this a lot to reveal something that should be read only for other
    files.

    "shantanu дµÀ£º
    "
    > Hi,
    > How can we declare a static variable or function declared in one
    > source file, which is to be used in other source file?
    Miles Davis, Nov 28, 2006
    #5
  6. shantanu

    Guest

    Is this interview question ?

    static is having File Scope. and lifetime up to prog End.

    What do you mean by "Used" ? u may used by pointer and all but not
    directly in different files.

    --raxit sheth




    shantanu wrote:
    > Hi,
    > How can we declare a static variable or function declared in one
    > source file, which is to be used in other source file?
    , Nov 28, 2006
    #6
  7. shantanu

    Miles Davis Guest

    " дµÀ£º
    "
    > Is this interview question ?
    >
    > static is having File Scope. and lifetime up to prog End.
    >
    > What do you mean by "Used" ? u may used by pointer and all but not
    > directly in different files.


    Undoubtedly, static var can not be accessed directly in different
    files. I meaned by using a pointer, we could access the var with some
    limitations (such as readonly attribute).

    >
    > --raxit sheth
    >
    Miles Davis, Nov 28, 2006
    #7
  8. shantanu

    sololoquist Guest

    Christopher Benson-Manica wrote:
    > shantanu <> wrote:
    >
    > > How can we declare a static variable or function declared in one
    > > source file, which is to be used in other source file?

    >
    > 1) Don't declare them to be static.
    >
    > 2) Add extern declarations to every source file except the one where you
    > define the function:
    >
    > A.c
    >
    > void foo() {
    > /* ... */
    > }
    >
    > B.c
    >
    > extern void foo;
    >
    > void bar() {
    > /* ... */
    > foo();
    > }
    >
    > How to correctly invoke your linker to make sure this works is a topic
    > for a different newsgroup.
    >
    > --
    > C. Benson Manica | I *should* know what I'm talking about - if I
    > cbmanica(at)gmail.com | don't, I need to know. Flames welcome.

    Is it not necessary to make
    extern void foo;
    into
    extern void foo();
    are the parenthesis unnecessary when used with extern like for an array
    the size ??
    please elaborate. thanx
    sololoquist, Nov 28, 2006
    #8
  9. shantanu

    Jack Klein Guest

    On 28 Nov 2006 06:29:36 -0800, "sololoquist"
    <> wrote in comp.lang.c:

    >
    > Christopher Benson-Manica wrote:
    > > shantanu <> wrote:
    > >
    > > > How can we declare a static variable or function declared in one
    > > > source file, which is to be used in other source file?

    > >
    > > 1) Don't declare them to be static.
    > >
    > > 2) Add extern declarations to every source file except the one where you
    > > define the function:
    > >
    > > A.c
    > >
    > > void foo() {
    > > /* ... */
    > > }
    > >
    > > B.c
    > >
    > > extern void foo;
    > >
    > > void bar() {
    > > /* ... */
    > > foo();
    > > }
    > >
    > > How to correctly invoke your linker to make sure this works is a topic
    > > for a different newsgroup.
    > >
    > > --
    > > C. Benson Manica | I *should* know what I'm talking about - if I
    > > cbmanica(at)gmail.com | don't, I need to know. Flames welcome.

    > Is it not necessary to make
    > extern void foo;
    > into
    > extern void foo();
    > are the parenthesis unnecessary when used with extern like for an array
    > the size ??
    > please elaborate. thanx


    You are correct, Christopher made a mistake.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 28, 2006
    #9
  10. shantanu

    santosh Guest

    sololoquist wrote:
    > Christopher Benson-Manica wrote:
    > > shantanu <> wrote:
    > >
    > > > How can we declare a static variable or function declared in one
    > > > source file, which is to be used in other source file?

    > >
    > > 1) Don't declare them to be static.
    > >
    > > 2) Add extern declarations to every source file except the one where you
    > > define the function:
    > >
    > > A.c
    > >
    > > void foo() {
    > > /* ... */
    > > }
    > >
    > > B.c
    > >
    > > extern void foo;
    > >
    > > void bar() {
    > > /* ... */
    > > foo();
    > > }
    > >
    > > How to correctly invoke your linker to make sure this works is a topic
    > > for a different newsgroup.

    >
    > Is it not necessary to make
    > extern void foo;
    > into
    > extern void foo();


    Yes. It was probably a typo.

    PS. Don't quote the sig unless you're commenting on it.
    santosh, Nov 28, 2006
    #10
  11. sololoquist <> wrote:

    > Is it not necessary to make
    > extern void foo;
    > into
    > extern void foo();
    > are the parenthesis unnecessary when used with extern like for an array
    > the size ??


    > please elaborate. thanx


    With pleasure; I blew it, and you are right - the parentheses are
    necessary. I'm glad you caught the error. No more late night posts
    (until next time).

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Nov 28, 2006
    #11
    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. =?Utf-8?B?QXNoYSBHaWxs?=

    Sharing variables between pages...

    =?Utf-8?B?QXNoYSBHaWxs?=, Jun 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,872
    Pete Wright
    Jun 9, 2004
  2. darrel
    Replies:
    3
    Views:
    370
    Kyril Magnos
    Jul 15, 2004
  3. W Akthar
    Replies:
    4
    Views:
    525
    Kevin Spencer
    Nov 4, 2004
  4. Replies:
    8
    Views:
    449
    Robbe Morris [C# MVP]
    Apr 5, 2006
  5. Rahul
    Replies:
    5
    Views:
    620
    James Kanze
    Jan 3, 2008
Loading...

Share This Page