From: Nate Eldredge Newsgroups: comp.os.msdos.djgpp Subject: Re: URGENT!!! Please (A Little Long) Date: Mon, 25 Oct 1999 12:56:40 -0700 Organization: Harvey Mudd College Lines: 79 Message-ID: <3814B5F8.772D1EAD@hmc.edu> References: <3813e85f DOT 18243975 AT news DOT pasteur DOT dialix DOT com DOT au> <004aa0e3 DOT faebcda4 AT usw-ex0101-006 DOT remarq DOT com> <381434da DOT 7867638 AT news DOT pasteur DOT dialix DOT com DOT au> NNTP-Posting-Host: mercury.st.hmc.edu Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: nntp1.interworld.net 940881452 84959 134.173.45.219 (25 Oct 1999 19:57:32 GMT) X-Complaints-To: usenet AT nntp1 DOT interworld DOT net NNTP-Posting-Date: 25 Oct 1999 19:57:32 GMT X-Mailer: Mozilla 4.61 [en] (X11; U; Linux 2.2.13pre12 i586) X-Accept-Language: en To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Kieran Farrell wrote: > > Ack Sorry guys, first rule in asking questions on newsgroups. Never > assume people know what you're asking *8) > > I made a simple question long winded. In short I'm asking how do I > pass a pointer by refference. Examples for this are opening files, and > adding nodes to linked lists. the later is what I'm trying to achieve. > However heres the file example, it might make more sense. > > int main(void) > { > FILE *fp; > > OpenFile(fp); > return 0; > } > > void OpenFile(FILE *fp) > { > fp = fopen('a:\\yadayad.txt", "wb"); > return; > } > > The above wont work, cause whatever pointer is assigned to fp is lost > when the OpenFile procedure is finished. You pass a pointer by reference the same as you pass any other type by reference: by passing a pointer to it. int main(void) { FILE *fp; OpenFile(&fp); return 0; } void OpenFile(FILE **fp) { *fp = fopen('a:\\yadayad.txt", "wb"); return; } However... > OK I'll try showing my quick fix again, hopefully it makes more sense > and should compile this time. > > int main(void) > { > FILE *fp; > > fp = OpenFile(fp); > return; > } > > FILE *OpenFile(FILE *fp) > { > fp = fopen('a:\\yadayad.txt", "wb"); > return fp; > } This is a much cleaner design. It's IMHO preferable to avoid having functions modify their arguments when possible. Actually, in this case, passing fp to OpenFile is unnecessary, since you never use it. It could be: FILE *OpenFile(void) { FILE *fp; fp = fopen(etc, etc); return fp; } -- Nate Eldredge neldredge AT hmc DOT edu