home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / sys / mac / programm / 21984 < prev    next >
Encoding:
Text File  |  1993-01-23  |  3.2 KB  |  74 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!yale!gumby!kzoo!k044477
  3. From: k044477@hobbes.kzoo.edu (Jamie R. McCarthy)
  4. Subject: Re: &StringPtr -vs- StringPtr?
  5. Message-ID: <1993Jan22.220940.21096@hobbes.kzoo.edu>
  6. Organization: Kalamazoo College
  7. References: <1993Jan22.165348.11313@novell.com> <1993Jan22.200837.13294@kronos.arc.nasa.gov>
  8. Date: Fri, 22 Jan 1993 22:09:40 GMT
  9. Lines: 63
  10.  
  11. joshr@kronos.arc.nasa.gov (Joshua Rabinowitz-Summer-91) writes:
  12. >damurphy@wc.novell.com (Duane Murphy) writes:
  13. >>
  14. >>I have received some code...  The MPW compiler says nothing, but
  15. >>Think C whines about types not matching.  Here is where the problem is:
  16. >>
  17. >>    Str255    msg1, msg2;
  18. >>    GetIndString(&msg1, errResID, errStringIndex);
  19. >>
  20. >>The problem is &msg1.  The prototype for GetIndString is:
  21. >>(from ToolUtils.h)
  22. >>
  23. >>pascal void GetIndString(Str255 theString,short strListID,short index); 
  24. >>
  25. >>I have changed the call to:
  26. >>
  27. >>    GetIndString(msg1, errResID, errStringIndex);
  28.  
  29. The short explanation:  you're right.  "Str255 msg1" is exactly the same
  30. as "unsigned char msg1[256]", so you're passing an array.  When you want
  31. to do that in C, just put down the array name.
  32.  
  33. >Try someting like this (it works for me)
  34. >GetIndString((ConstStr255Param) msg1, ......
  35.  
  36. This shouldn't be necessary...
  37.  
  38. >Also, note that because msg1 is technically an array of chars, 
  39. >msg1 is the address of the first element, and &msg1 has no real
  40. >meaning (thoug usually is interpreted as just &msg1[0], or msg1).
  41.  
  42. Well, "&msg1" does have a well-defined meaning.  The expression "msg1"
  43. does have the type "pointer to unsigned char," and is equivalent to
  44. the more-logical-looking "&msg1[0]".  But "&msg1", while it points to
  45. the same place, has the type "pointer to array of 256 unsigned chars."
  46. That's not the same thing.
  47.  
  48. I would guess that, in this case, ThC had strict pointer type-checking
  49. on and MPW did not.  Or, maybe MPW forgives this particular mismatching
  50. of pointers (pointer to array of T instead of pointer to T), though I
  51. would be surprised if that were the case.
  52.  
  53. To quote gospel at you:  "If the type of an expression or subexpression
  54. is 'array of T,' for some type T, then the value of the expression is a
  55. pointer to the first object in the array, and the type of the expression
  56. is altered to 'pointer to T.'  This conversion does not take place if
  57. the expression is the operand of the unary & operator, or of ++, --,
  58. sizeof, or as the left operand of an assignment operator or the .
  59. operator."  (K&R, 2nd ed., A7.1, p. 200.)  So, "msg1" by itself is a
  60. pointer to unsigned char;  "&msg1" is a pointer to an array of 256
  61. unsigned chars;  "sizeof(msg1)" returns the size of the array instead of
  62. the size of a pointer;  "++msg1" increments the pointer 256 bytes
  63. instead of one, and so on.  Ain't C grand?
  64.  
  65. >I would say that if you passed msg1 (not &msg1), it SHOULD accept it.
  66. >I believe I usually must cast in that situation as well, though.
  67.  
  68. I don't think so.  At least, I don't have to, and I keep ThC on its
  69. strictest type-checking.
  70. -- 
  71.  Jamie McCarthy      Internet: k044477@kzoo.edu      AppleLink: j.mccarthy
  72.  "A degree in Computer Science from Berkeley didn't much qualify me to uphold
  73.   the banner of Chaos against the forces of Order."              - R. Zelazny
  74.