Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
I wrote this routine which is supposed to open a fi
I wrote this routine which is supposed to open a file:
myfopen(char *filename, FILE *fp)
{
fp = fopen(filename, "r");
}
But when I call it like this:
FILE *infp;
myfopen("filename.dat", infp);
the infp variable in the caller doesn't get set properly.
✍: Guest
Functions in C always receive copies of their arguments, so a function can never ``return'' a value to the caller by assigning to an argument.
For this example, one fix is to change myfopen to return a FILE *:
FILE *myfopen(char *filename)
{
FILE *fp = fopen(filename, "r");
return fp;
}
and call it like this:
FILE *infp;
infp = myfopen("filename.dat");
Alternatively, have myfopen accept a pointer to a FILE * (a pointer-to-pointer-to-FILE):
myfopen(char *filename, FILE **fpp)
{
FILE *fp = fopen(filename, "r");
*fpp = fp;
}
and call it like this:
FILE *infp;
myfopen("filename.dat", &infp);
2015-10-09, 1311👍, 0💬
Popular Posts:
What Happens If One Row Has Missing Columns? - XHTML 1.0 Tutorials - Understanding Tables and Table ...
Give me the example of SRS and FRS SRS :- Software Requirement Specification BRS :- Basic Requiremen...
How to reduce the final size of an executable file? Size of the final execuatable can be reduced usi...
Advantages of a macro over a function? Macro gets to see the Compilation environment, so it can expa...
What is XSLT? XSLT is a rule based language used to transform XML documents in to other file formats...