Article View: comp.sys.mac.programmer.help
Article #10Re: Pointer arithmetic in C?
From: Martin Adler
Date: Mon, 21 Aug 1995 11:28
Date: Mon, 21 Aug 1995 11:28
47 lines
2041 bytes
2041 bytes
In article <oylerb-2108950012310001@msp1-5.nas.mr.net>, oylerb@tdkt.skypoint.net (Bill A. Oyler) wrote: > Hello, > > Back in my Pascal days, I had absolutely no problem writing strings > (Str255s) to text files. I would just do something like this: > > TextToWrite := 'hello, world!'; > Count := Length(TextToWrite) - 1; > OSError := SetFPos(LogFileRefNum, fsFromLEOF, 0); > OSError := FSWrite(LogFileRefNum, Count, @TextToWrite[1]); > > What would be the C equivalent of this code?? > > Asside from the obvious syntax differences, all I changed was > "@TextToWrite[1]" to "&TextToWrite + 1". Is there something more I need > to do? I keep getting garble in my text file.... > > > Try: > ... > OSError = SetFPos(LogFileRefNum, fsFromLEOF, 0); > OSError = FSWrite(LogFileRefNum, Count,(Ptr) &(TextToWrite[1])); > ... > > this should work, also i didn't try it Sorry for "also", it was my f.. english! your version "&TextToWrite + 1" didn't work because in C the var "TextToWrite" is the pointer to the pascal-string. So, your assignment "&TextToWrite" is an ptr to a ptr to the pascal-string, this can't work. for details, you should read the pointer/array-chapter of any c-book. the following line should be equivalent: OSError = FSWrite(LogFileRefNum, Count,(Ptr) &(TextToWrite[1])); OSError = FSWrite(LogFileRefNum, Count,(Ptr) (TextToWrite + 1)); (i like to write more brackets and typecoercing than really needed, but i think this makes the code more readable) Gulpi = Martin Adler
Technical Details
Message-ID:
<adler-2108951328000001@138.244.207.201>Path:
arc1-ca.pugleaf.net!lrz-muenchen.de!NewsWatcher!userReferences:
<oylerb-2108950012310001@msp1-5.nas.mr.net>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!lrz-muenchen.de!NewsWatcher!user From: adler@informatik.tu-muenchen.de (Martin Adler) Newsgroups: comp.sys.mac.programmer.help,comp.lang.c Subject: Re: Pointer arithmetic in C? Date: Mon, 21 Aug 1995 13:28:00 +0200 Organization: Leibniz-Rechenzentrum, Muenchen (Germany) Distribution: world Message-ID: <adler-2108951328000001@138.244.207.201> References: <oylerb-2108950012310001@msp1-5.nas.mr.net> NNTP-Posting-Host: 138.244.207.201