View Full Version : Use of Static function
Lloyd
04-10-2006, 11:15 AM
Hi,
What is the real use of static functions in 'C'?
Another doubt is why do we use '&' (Address operator) in scanf function but not in printf ?
RobSeace
04-10-2006, 12:20 PM
Static functions are used when you only need to call them from within the single
source file they are defined in... They are not callable from outside, and therefore
can serve to hide internal details of your implementation without needing them to
become part of the external API... And, reducing the number of unnecessary
external symbols can lead to more efficiency (though probably not much worth
mentioning)... Also, compilers may be able to optimize more effectively with proper
use of static for functions you don't need to call externally... (Eg: they can optimize
them completely out of existence if never called at all...)
You need to use "&" in *scanf() because it is filling in variables, whereas *printf() is
merely printing them out... All arguments in plain C are pass-by-value, so the only
way to have a function modify its arguments is to have them be pointers to the
variables to actually modify... Otherwise, all it'd be modifying would be its own local
stack copies of the variables, which wouldn't be that productive... But, since you
pass in the address of the variables as pointers, it can simply dereference those
pointers, and modify the variables themselves...
Lloyd
05-01-2006, 10:29 AM
FILE *fp=fopen(......);
cout<<fp->_tmpfname;
Why cant we access (at least to read) the members of FILE structure? The above code does not cause any compilter error but run time error!
i3839
05-01-2006, 12:35 PM
FILE is supposed to be an opaque data type, you shouldn't poke at it the way you do at all.
That you don't get compile errors doesn't give you any guarantee that your version of libc is using the same FILE structure as the headers tell you. Compile it with -Wall -W and check if you don't get warnings. At least on my system FILE is defined in libio.h and has no member _tmpfname.
#include <stdio.h>
int main()
{
FILE* bla = fopen("/tmp/main.c", "r");
printf("%i\n", bla->_fileno);
return 3;
}
This works and prints out "3". (But you could get the same by calling fileno(bla)...)
Lloyd
05-02-2006, 04:59 AM
Thanks, but this code is not working in machine. (it causes segmentation fault at run time) And also I am not able to find the sructure of FILE in libio.h. I am using gcc on a redhat EL.
RobSeace
05-02-2006, 04:06 PM
Just what exactly are you trying to do?? Like i3839 said, the contents of the "FILE"
struct are intended to be hidden from you, and you really should not go poking
around inside it... Of course, you CAN if you know what you're doing, but if you
don't, well then you can expect bad things to happen... And, in general, I can't
really think of much of any reason to want to mess around with the internals of a
FILE struct, anyway... So, what exactly are you trying to accomplish that you think
requires you to do so??
Lloyd
05-03-2006, 04:46 AM
I just want to write some simple programs based on that, nothing else. Because I would like to know its (FILE) internals and how it is handled in the system.
RobSeace
05-03-2006, 02:22 PM
Well, then your best bet is to probably look through the glibc source, and see what
it's doing... I think the internal glibc notion of a FILE struct differs from that presented
to the outside world, as well... (Which, as i3839 mentioned, is defined in <libio.h>...
You might not spot it, because it's defined as "struct _IO_FILE", but you'll see in
<stdio.h> that it just typedefs that struct to be "FILE"...)
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.