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, 1296👍, 0💬
Popular Posts:
What is the version information in XML? “version” tag shows which version of XML is used.
In below sample code if we create a object of class2 which constructor will fire first? Public Class...
What Does a HTML Document Look Like? A HTML document is a normal text file with predefined tags mixe...
What exactly happens when ASPX page is requested from Browser? Note: - Here the interviewer is expec...
.NET INTERVIEW QUESTIONS - What is the difference between thread and process? A thread is a path of ...