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, 1294👍, 0💬
Popular Posts:
How To Export Your Connection Information to a File? - Oracle DBA FAQ - Introduction to Oracle SQL D...
What is COCOMO I, COCOMOII and COCOMOIII? In CD we have a complete free PDF tutorial of how to prepa...
Which bit wise operator is suitable for turning off a particular bit in a number? The bitwise AND op...
Can static variables be declared in a header file? You can't declare a static variable without defin...
Do events have return type ? No, events do not have return type.