Thread View: comp.sys.mac.programmer.help
5 messages
5 total messages
Started by Bill A. Oyler
Mon, 21 Aug 1995 05:12
Pointer arithmetic in C?
Author: Bill A. Oyler
Date: Mon, 21 Aug 1995 05:12
Date: Mon, 21 Aug 1995 05:12
23 lines
1230 bytes
1230 bytes
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....
Thanks!!
=====================================================
Bill A. Oyler People usually get what's
Minnetonka, MN, USA coming to them ... unless
oylerb@tdkt.skypoint.net it's been mailed.
=====================================================
Re: Pointer arithmetic in C?
Author: Martin Adler
Date: Mon, 21 Aug 1995 10:34
Date: Mon, 21 Aug 1995 10:34
33 lines
1413 bytes
1413 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.... > > Thanks!! > Try: ... OSError = SetFPos(LogFileRefNum, fsFromLEOF, 0); OSError = FSWrite(LogFileRefNum, Count,(Ptr) &(TextToWrite[1])); ... this should work, also i didn't try it Gulpi = Martin Adler
Re: Pointer arithmetic in C?
Author: 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
Re: Pointer arithmetic in C?
Author: John Caulfield
Date: Mon, 21 Aug 1995 12:21
Date: Mon, 21 Aug 1995 12:21
35 lines
1681 bytes
1681 bytes
In article <adler-2108951328000001@138.244.207.201> adler@informatik.tu-muenchen.de (Martin Adler) writes:
>In article <oylerb-2108950012310001@msp1-5.nas.mr.net>,
>> 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!';
<snip>
>the following line should be equivalent:
>OSError = FSWrite(LogFileRefNum, Count,(Ptr) &(TextToWrite[1]));
>OSError = FSWrite(LogFileRefNum, Count,(Ptr) (TextToWrite + 1));
<snip>
Assuming that the assignment above was translated into C as
TextToWrite = "hello, world!";
you are passing FSWrite() as it's third parameter a pointer to the string
"ello, world!". Are you _sure_ that's what you want?
Lose the [1] or + 1, which means also lose the & and the parens. Adding one
may have been necessary in Pascal because of the count byte in the string. C
doesn't have one.
Regards,
jpc
John Caulfield
johnpc@omni.voicenet.com
Don't put that in your mouth. You
don't know where it's been. - Your mother
Re: Pointer arithmetic in C?
Author: Martin Adler
Date: Mon, 21 Aug 1995 13:07
Date: Mon, 21 Aug 1995 13:07
66 lines
2620 bytes
2620 bytes
In article <johnpc.39.075ECBAC@omni.voicenet.com>, johnpc@omni.voicenet.com (John Caulfield) wrote: > In article <adler-2108951328000001@138.244.207.201> adler@informatik.tu-muenchen.de (Martin Adler) writes: > >In article <oylerb-2108950012310001@msp1-5.nas.mr.net>, > >> 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!'; > > <snip> > > >the following line should be equivalent: > > >OSError = FSWrite(LogFileRefNum, Count,(Ptr) &(TextToWrite[1])); > >OSError = FSWrite(LogFileRefNum, Count,(Ptr) (TextToWrite + 1)); > > <snip> > > Assuming that the assignment above was translated into C as > > TextToWrite = "hello, world!"; > > you are passing FSWrite() as it's third parameter a pointer to the string > "ello, world!". Are you _sure_ that's what you want? > > Lose the [1] or + 1, which means also lose the & and the parens. Adding one > may have been necessary in Pascal because of the count byte in the string. C > doesn't have one. > > Regards, > jpc > ok, ok, ... i didn't realize exactly the "TextToWrite := 'hello, world!';"-line, so you're right, my solution is only right for a pascal-string. i only looked at the header: > >> Back in my Pascal days, I had absolutely no problem writing strings > >> (Str255s) to text files. I would just do something like this: correct solution: ... OSErr OSError; char TextToWrite[14] = 'hello, world!'; // 14 = 13 chars from 'hello, world!' + 1 char delimiter long Count = strlen(TextToWrite); OSError = SetFPos(LogFileRefNum, fsFromLEOF, 0); OSError = FSWrite(LogFileRefNum, Count, TextToWrite); or if TextToWrite is a pascal-string (often returned by toolbox-routines): ... OSErr OSError; long Count = TextToWrite[0]; OSError = SetFPos(LogFileRefNum, fsFromLEOF, 0); OSError = FSWrite(LogFileRefNum, Count,(Ptr) (TextToWrite + 1)); now i hope it's ok... Gulpi = Martin Adler
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads