home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.mac.programmer
- Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!yale!gumby!kzoo!k044477
- From: k044477@hobbes.kzoo.edu (Jamie R. McCarthy)
- Subject: Re: &StringPtr -vs- StringPtr?
- Message-ID: <1993Jan22.220940.21096@hobbes.kzoo.edu>
- Organization: Kalamazoo College
- References: <1993Jan22.165348.11313@novell.com> <1993Jan22.200837.13294@kronos.arc.nasa.gov>
- Date: Fri, 22 Jan 1993 22:09:40 GMT
- Lines: 63
-
- joshr@kronos.arc.nasa.gov (Joshua Rabinowitz-Summer-91) writes:
- >damurphy@wc.novell.com (Duane Murphy) writes:
- >>
- >>I have received some code... The MPW compiler says nothing, but
- >>Think C whines about types not matching. Here is where the problem is:
- >>
- >> Str255 msg1, msg2;
- >> GetIndString(&msg1, errResID, errStringIndex);
- >>
- >>The problem is &msg1. The prototype for GetIndString is:
- >>(from ToolUtils.h)
- >>
- >>pascal void GetIndString(Str255 theString,short strListID,short index);
- >>
- >>I have changed the call to:
- >>
- >> GetIndString(msg1, errResID, errStringIndex);
-
- The short explanation: you're right. "Str255 msg1" is exactly the same
- as "unsigned char msg1[256]", so you're passing an array. When you want
- to do that in C, just put down the array name.
-
- >Try someting like this (it works for me)
- >GetIndString((ConstStr255Param) msg1, ......
-
- This shouldn't be necessary...
-
- >Also, note that because msg1 is technically an array of chars,
- >msg1 is the address of the first element, and &msg1 has no real
- >meaning (thoug usually is interpreted as just &msg1[0], or msg1).
-
- Well, "&msg1" does have a well-defined meaning. The expression "msg1"
- does have the type "pointer to unsigned char," and is equivalent to
- the more-logical-looking "&msg1[0]". But "&msg1", while it points to
- the same place, has the type "pointer to array of 256 unsigned chars."
- That's not the same thing.
-
- I would guess that, in this case, ThC had strict pointer type-checking
- on and MPW did not. Or, maybe MPW forgives this particular mismatching
- of pointers (pointer to array of T instead of pointer to T), though I
- would be surprised if that were the case.
-
- To quote gospel at you: "If the type of an expression or subexpression
- is 'array of T,' for some type T, then the value of the expression is a
- pointer to the first object in the array, and the type of the expression
- is altered to 'pointer to T.' This conversion does not take place if
- the expression is the operand of the unary & operator, or of ++, --,
- sizeof, or as the left operand of an assignment operator or the .
- operator." (K&R, 2nd ed., A7.1, p. 200.) So, "msg1" by itself is a
- pointer to unsigned char; "&msg1" is a pointer to an array of 256
- unsigned chars; "sizeof(msg1)" returns the size of the array instead of
- the size of a pointer; "++msg1" increments the pointer 256 bytes
- instead of one, and so on. Ain't C grand?
-
- >I would say that if you passed msg1 (not &msg1), it SHOULD accept it.
- >I believe I usually must cast in that situation as well, though.
-
- I don't think so. At least, I don't have to, and I keep ThC on its
- strictest type-checking.
- --
- Jamie McCarthy Internet: k044477@kzoo.edu AppleLink: j.mccarthy
- "A degree in Computer Science from Berkeley didn't much qualify me to uphold
- the banner of Chaos against the forces of Order." - R. Zelazny
-