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

J

jeniffer

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

jitu.csewizard

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

Vladimir S. Oka

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.
 
V

Vladimir S. Oka

(e-mail address removed) 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
 
M

Martin Ambuhl

jeniffer said:
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.
 
M

Martin Ambuhl

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.
 
K

Keith Thompson

jeniffer said:
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.)
 
J

jjf

Vladimir said:
(e-mail address removed) opined:

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.
 
V

Vladimir S. Oka

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.
 
S

suresh

Keith said:
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.
 
K

Keith Thompson

suresh said:
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.
 
R

Richard Tobin

jeniffer said:
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
 
J

Jordan Abel

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top