C/Function Prototype
Expert: Prince M. Premnath - 8/19/2009
QuestionQUESTION: FILE *fp; What exactly fp is going to stor and why it is declared as pointer?
ANSWER: Hai dear Prakash B M !
FILE is nothing but a structure defined in stdio.h header file with the following fields
typedef struct {
int level; /* fill/empty level of buffer */
unsigned flags; /* File status flags */
char fd; /* File descriptor */
unsigned char hold; /* Ungetc char if no buffer */
int bsize; /* Buffer size */
unsigned char *buffer; /* Data transfer buffer */
unsigned char *curp; /* Current active pointer */
unsigned istemp; /* Temporary file indicator */
short token; /* Used for validity checking */
} FILE;
( Note : Code Excerpt from stdio.h )
FILE* fp; will create a pointer to the FILE structure this pointer !
while opening a file using fopen(), this function will return a pointer of type FILE ,ie fp holds all the information about the file , this informations are vitally used in all operation regarding file processing , in C programming all the files will be given a unique handle ( say file pointer ) using this handle we can perform manipulations over a file
Thanks and regards !
Prince M. Premnath
---------- FOLLOW-UP ----------
QUESTION: if function definition appears before calling function, function prototype is not necessary. why?
AnswerHi Dear BM Prakash !
As you know well the execution is sequential starts from main() , any definition say MACRO , or global variables and function definitions or prototypes. if the compiler encounter any of the above said it will make a note on the symbol table which in turn will be used during program execution.
take a look at this small prog
#include<stdio.h>
void foo()
{
..
}
void main()
{
foo();
}
During compilation first compiler will encounter void foo() ,internally it will create a description as states that foo() is a user defined function which will return nothing ( in its own format ) During the execution phase it will encounter foo() to gain more information regarding foo() it will look for symbol table and dictionary , there it will fetch all the information related to execution , if the definition is not avail then it cannot proceed its execution further and obviously will lead to crash
To avoid the above mentioned issue during the compilation phase it will look for all the essential details for execution in terms of function at least a function prototype is essential before its called for execution, compiler will store the information such as function name , return type, If the function prototype founds missing it will report you with an error.
If the function is defined before its call, then compiler will get both its declaration and definition information from the same so there is no need of separate function prototype if the function is defined before its call.
Hope this may help you, get back to me for more queries.
Thanks and Regards!
Prince M. Premnath