Can a static function defined in a C file be ever referred (called) externally ?

Discussion in 'C Programming' started by jeniffer, Mar 29, 2006.

  1. jeniffer

    jeniffer Guest

    Can a static function defined in a C file be ever referred (called)
    externally from another C file?If so in which conditions?
    jeniffer, Mar 29, 2006
    #1
    1. Advertising

  2. jeniffer

    Guest

    only when you include the file(in which static function is defined) in
    another C file(in which you want to use)
    , Mar 29, 2006
    #2
    1. Advertising

  3. jeniffer opined:

    > Can a static function defined in a C file be ever referred (called)
    > externally from another C file?If so in which conditions?


    Yes, if you declare a variable to be a pointer to such a function, and
    make it point to that function (obviously this has to be done in the
    file where `static` function is). If that pointer is not static, it
    can be used from elsewhere to call that function.

    It may help if you think of function /names/, they're not accessible.

    --
    BR, Vladimir

    INTOXICATED:
    When you feel sophisticated without being able to pronounce it.
    Vladimir S. Oka, Mar 29, 2006
    #3
  4. opined:

    > only when you include the file(in which static function is defined)
    > in another C file(in which you want to use)


    What are you talking about? Who are you talking to? Read:

    <http://cfaj.freeshell.org/google/>
    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

    Also, your answer is wrong. Including a file into another makes it a
    part of that file, and thus irrelevant to the original question. See
    my other post for the correct answer.

    --
    BR, Vladimir

    "I know not with what weapons World War III will be fought, but World
    War IV will be fought with sticks and stones."
    -- Albert Einstein
    Vladimir S. Oka, Mar 29, 2006
    #4
  5. Re: Can a static function defined in a C file be ever referred (called)externally ?

    jeniffer wrote:
    > Can a static function defined in a C file be ever referred (called)
    > externally from another C file?If so in which conditions?


    Yes. Through a pointer-to-function that has been assigned the address
    of the static function. The pointer must have external linkage and the
    assignment done in the compilation unit containing the static function.

    This allows a compilation unit to have several candidate static
    functions and a function which can choose which one is to be used.
    Martin Ambuhl, Mar 29, 2006
    #5
  6. Re: Can a static function defined in a C file be ever referred (called)externally ?

    wrote:
    > only when you include the file(in which static function is defined) in
    > another C file(in which you want to use)


    This is incorrect. A pointer-to-function with external linkage will do
    the job.
    Martin Ambuhl, Mar 29, 2006
    #6
  7. Re: Can a static function defined in a C file be ever referred(called) externally ?

    "jeniffer" <> writes:
    > Can a static function defined in a C file be ever referred (called)
    > externally from another C file?If so in which conditions?


    Is this a homework question?

    If you want to call a function from another C file, don't make it
    static.

    (That doesn't answer your original question.)

    --
    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, Mar 29, 2006
    #7
  8. jeniffer

    Guest

    Vladimir S. Oka wrote:
    > opined:
    > > jeniffer wrote:
    > > > Can a static function defined in a C file be ever referred (called)
    > > > externally from another C file?If so in which conditions?

    > >
    > > only when you include the file(in which static function is defined)
    > > in another C file(in which you want to use)

    >
    > your answer is wrong. Including a file into another makes it a
    > part of that file, and thus irrelevant to the original question. See
    > my other post for the correct answer.


    No, it's still a separate file but it becomes part of the same
    translation unit. The information is correct, but it's not the "only"
    way.
    , Mar 29, 2006
    #8
  9. wrote:
    > Vladimir S. Oka wrote:
    > > opined:
    > > > jeniffer wrote:
    > > > > Can a static function defined in a C file be ever referred (called)
    > > > > externally from another C file?If so in which conditions?
    > > >
    > > > only when you include the file(in which static function is defined)
    > > > in another C file(in which you want to use)

    > >
    > > your answer is wrong. Including a file into another makes it a
    > > part of that file, and thus irrelevant to the original question. See
    > > my other post for the correct answer.

    >
    > No, it's still a separate file but it becomes part of the same
    > translation unit. The information is correct, but it's not the "only"
    > way.


    Re-read the OP above.

    --
    BR, Vladimir
    Vladimir S. Oka, Mar 29, 2006
    #9
  10. jeniffer

    suresh Guest

    Keith Thompson wrote:
    > "jeniffer" <> writes:
    > > Can a static function defined in a C file be ever referred (called)
    > > externally from another C file?If so in which conditions?

    >
    > Is this a homework question?
    >
    > If you want to call a function from another C file, don't make it
    > static.
    >


    This situation is not just academic. Consider a callback function
    implementation.
    suresh, Mar 29, 2006
    #10
  11. Re: Can a static function defined in a C file be ever referred(called) externally ?

    "suresh" <> writes:
    > Keith Thompson wrote:
    >> "jeniffer" <> writes:
    >> > Can a static function defined in a C file be ever referred (called)
    >> > externally from another C file?If so in which conditions?

    >>
    >> Is this a homework question?
    >>
    >> If you want to call a function from another C file, don't make it
    >> static.

    >
    > This situation is not just academic. Consider a callback function
    > implementation.


    Sure, there are real-world reasons to call a static function from a
    different translation unit. But the way the question was phrased
    leads me to suspect it's a homework question.

    --
    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, Mar 29, 2006
    #11
  12. In article <>,
    jeniffer <> wrote:

    >Can a static function defined in a C file be ever referred (called)
    >externally from another C file?If so in which conditions?


    Yes. The static declaration limits the scope of the function name,
    but not that of the function itself.

    -- Richard
    Richard Tobin, Mar 29, 2006
    #12
  13. jeniffer

    Jordan Abel Guest

    On 2006-03-29, Martin Ambuhl <> wrote:
    > wrote:
    >> only when you include the file(in which static function is defined) in
    >> another C file(in which you want to use)

    >
    > This is incorrect. A pointer-to-function with external linkage will do
    > the job.


    Yes, but a pointer-to-function being passed as a parameter is more
    likely in the real world.
    Jordan Abel, Mar 29, 2006
    #13
    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. Michael Tholkes
    Replies:
    2
    Views:
    418
    Yan-Hong Huang[MSFT]
    Jun 30, 2003
  2. Jody Burgess
    Replies:
    2
    Views:
    538
    George Kinney
    Jul 17, 2004
  3. Terry Hancock

    Externally-defined properties?

    Terry Hancock, Aug 24, 2005, in forum: Python
    Replies:
    3
    Views:
    286
    Michele Simionato
    Aug 25, 2005
  4. Replies:
    1
    Views:
    277
    Sion Arrowsmith
    Sep 28, 2006
  5. lovecreatesbeauty
    Replies:
    2
    Views:
    355
    lovecreatesbeauty
    Jul 20, 2011
Loading...

Share This Page