🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.mac.programmer.help
Article #18

Re: Pointer arithmetic in C?

#18
From: Martin Adler
Date: Mon, 21 Aug 1995 13:07
66 lines
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
Technical Details
Message-ID: <adler-2108951507550001@138.244.207.201>
Path: arc1-ca.pugleaf.net!lrz-muenchen.de!NewsWatcher!user
References: <oylerb-2108950012310001@msp1-5.nas.mr.net> <adler-2108951328000001@138.244.207.201> <johnpc.39.075ECBAC@omni.voicenet.com>

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 15:07:55 +0200
Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
Distribution: world
Message-ID: <adler-2108951507550001@138.244.207.201>
References: <oylerb-2108950012310001@msp1-5.nas.mr.net> <adler-2108951328000001@138.244.207.201> <johnpc.39.075ECBAC@omni.voicenet.com>
NNTP-Posting-Host: 138.244.207.201