[comp.sys.mac.programmer] GetIndString

jas@cadre.dsl.PITTSBURGH.EDU (Jeffrey A. Sullivan) (03/28/88)

Well, I found out (sort of) what the problem is:  I am using TransDisplay for
output to the window, so bear that in mind.  The items DisplayLong writes
out the ascii of the long it's passed, DisplayString writes out the (pascal
style) string it gets.

Anyway, when I use getIndString, it uses the following call:

GetIndString(thestr, 1, 1);
where thestr is a StringPtr.  According to IM (I-468), "... if the resource
can't be read or the index [3rd param] is out of bounds, the empty string
is returned."  Here is where my problem comes, maybe.  I assumed the empty
string meant null, so I did this:

GetIndString(thestr, 1, 1); 
if(thestr)
	DisplayString;

but always thestr was 0L (I tested this by writing:

if(thestr) {
	DisplayString(thestr);
	} else {
	DisplayLong((long)thestr);
}

and every time, it printed out "0").

Why is thishappening?  I assume that "the empty string" must mean something
other than 0L, so how do I check for it, and why would the pointer (not
indirected at all, the _pointer_) have a value of 0?  This makes no sense to
me.  If two StringPtr's print out the same numeric value, they _must_ print/
refer to the same memory location, yes?

Flustered, but my code works,


-- 
..........................................................................
Jeffrey Sullivan			  | University of Pittsburgh
jas@cadre.dsl.pittsburgh.edu		  | Intelligent Systems Studies Program
jasper@PittVMS.BITNET, jasst3@cisunx.UUCP | Graduate Student

rs4u+@andrew.cmu.edu (Richard Siegel) (04/04/88)

The argument to GetIndString isn't a StringPtr per se, rather, it's
the *address* of an Str255. If you're passing an unitialized StringPtr
to GetIndString, anything can (and will!) happen.

So consider:

Str255 s;
/* other stuff */

GetIndString(&s, foo, bar);

If the string couldn't be found, then the string will be of zero-length
-- NOT NULL! -- so its first byte will be zero:

	if (!s[0]) {/* string is of nonzero length*/}

Hope this works OK for you...

	-R.