how to use static function defined in one file in another file is that impposiible in 'c '

Discussion in 'C Programming' started by rashmi, Apr 29, 2005.

  1. rashmi

    rashmi Guest

    how to use static function defined in one file in another file is that
    impposiible in 'c '
     
    rashmi, Apr 29, 2005
    #1
    1. Advertising

  2. Re: how to use static function defined in one file in another fileis that impposiible in 'c '

    rashmi wrote:
    > how to use static function defined in one file in another file is that
    > impposiible in 'c '
    >


    /* file b.c */
    /* hidden function */
    static int local_foo(int x) {
    return x*x;
    }

    /* function with external linkage */
    int foo(int x) {
    return local_foo(x);
    }

    /* function pointer with external linkage */
    int (*foop)(int) = local_foo;

    /* file a.c */
    #include <stdio.h>
    int foo(int);
    int (*foop)(int);

    int main(void)
    {
    int x = 2;
    printf("foo(%d) = ",x);
    printf("%d\b", x = foo(x));
    printf("foop(%d) = ",x);
    printf("%d\b", x = foop(x));
    return 0;
    }

    [output]
    foo(2) = 4
    foop(4) = 16
     
    Martin Ambuhl, Apr 29, 2005
    #2
    1. Advertising

  3. rashmi

    CBFalconer Guest

    Re: how to use static function defined in one file in another file isthat impposiible in 'c '

    rashmi wrote:
    >
    > how to use static function defined in one file in another file is
    > that impposiible in 'c '


    You can only do that by passing a pointer to the function to some
    other function. Look at the way my hashlib is configured via the
    hshinit function. The function pointers passed may be static in
    the calling module, and in fact often are. See:

    <http://cbfalconer.home.att.net/download/>

    Static, when applied to a function, means that the name is not
    externally available for linking, and thus will not create
    namespace conflicts. The code itself remains accessible.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Apr 29, 2005
    #3
  4. Re: how to use static function defined in one file in another fileis that impposiible in 'c '

    "rashmi" <> writes:
    > how to use static function defined in one file in another file is that
    > impposiible in 'c '


    If you can obtain a pointer to the function, you can call it through
    the pointer. Or if you really mean "file" rather than "translation
    unit", you can #include the file containing the function definition
    (which is almost certainly not what you're asking for and not a good
    idea).

    But basically the point of declaring a function static is so that it
    *can't* be called from outside the translation unit that defines it.

    Why do you want to do this?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 29, 2005
    #4
  5. rashmi wrote on 29/04/05 :
    > how to use static function defined in one file in another file is that
    > impposiible in 'c '


    It's possible via a pointer to function.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "There are 10 types of people in the world today;
    those that understand binary, and those that dont."
     
    Emmanuel Delahaye, Apr 29, 2005
    #5
  6. rashmi

    Guest

    rashmi wrote:
    > how to use static function defined in one file in another file is

    that
    > impposiible in 'c '


    You could always have a wrapper function in the same file containing
    the static function. This function would be of external linkage and
    would invoke the static function internally.
    So, you would have two functions in this file:
    static my_static_fun{};
    and the wrapper function
    my_static wrapper
    {
    my_static_fun();
    };

    Then, you just include a header file containing the prototype of this
    function in the other file from which you want accs to the static fun.
     
    , Apr 29, 2005
    #6
    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. Muhammad Khan
    Replies:
    4
    Views:
    1,255
    Mike Treseler
    Jul 10, 2003
  2. Oodini
    Replies:
    1
    Views:
    1,836
    Keith Thompson
    Sep 27, 2005
  3. Mitchell L Model
    Replies:
    2
    Views:
    350
    Steven D'Aprano
    Jan 8, 2010
  4. Alok
    Replies:
    3
    Views:
    285
  5. Replies:
    6
    Views:
    380
    Eric Sosman
    Oct 24, 2012
Loading...

Share This Page