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, 1241👍, 0💬
Popular Posts:
.NET INTERVIEW QUESTIONS - What is the difference between System exceptions and Application exceptio...
How To Define a Data Source Name (DSN) in ODBC Manager? - Oracle DBA FAQ - ODBC Drivers, DSN Configu...
What Tools to Use to View HTML Documents? The basic tool you need to view HTML documents is any Web ...
.NET INTERVIEW QUESTIONS - How to prevent my .NET DLL to be decompiled? By design .NET embeds rich M...
How To Specify Two Background Images on a Page? - CSS Tutorials - Page Layout and Background Image D...