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

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

P: n/a
how to use static function defined in one file in another file is that
impposiible in 'c '

Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Nov 14 '05 #2

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

Nov 14 '05 #3

P: n/a
"rashmi" <ns******@gmail.com> 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) 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.
Nov 14 '05 #4

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

Nov 14 '05 #5

P: n/a

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.

Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.