By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,300 Members | 2,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,300 IT Pros & Developers. It's quick & easy.

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

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

Mar 29 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
only when you include the file(in which static function is defined) in
another C file(in which you want to use)

Mar 29 '06 #2

P: n/a
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.

Mar 29 '06 #3

P: n/a
ji************@gmail.com 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

Mar 29 '06 #4

P: n/a
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.
Mar 29 '06 #5

P: n/a
ji************@gmail.com 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.

Mar 29 '06 #6

P: n/a
"jeniffer" <ze******************@gmail.com> 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) ks***@mib.org <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.
Mar 29 '06 #7

P: n/a
jjf

Vladimir S. Oka wrote:
ji************@gmail.com 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 '06 #8

P: n/a

jj*@bcs.org.uk wrote:
Vladimir S. Oka wrote:
ji************@gmail.com 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

Mar 29 '06 #9

P: n/a
Keith Thompson wrote:
"jeniffer" <ze******************@gmail.com> 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.

Mar 29 '06 #10

P: n/a
"suresh" <ma**********@gmail.com> writes:
Keith Thompson wrote:
"jeniffer" <ze******************@gmail.com> 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) ks***@mib.org <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.
Mar 29 '06 #11

P: n/a
In article <11**********************@g10g2000cwb.googlegroups .com>,
jeniffer <ze******************@gmail.com> 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

Mar 29 '06 #12

P: n/a
On 2006-03-29, Martin Ambuhl <ma*****@earthlink.net> wrote:
ji************@gmail.com 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.
Mar 29 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.