Re: NSRectArray by reference
Re: NSRectArray by reference
- Subject: Re: NSRectArray by reference
- From: Uli Kusterer <email@hidden>
- Date: Sun, 9 Sep 2007 21:16:34 +0200
On 09.09.2007, at 19:45, Keith Duncan wrote:
(*rowRects[index]) = currentRowRect; // it crashes here (for
reasons that are probably obvious to the reader), notably the most
crucial line
currentRowRect.origin.y += delta;
This is kind of a short, and probably confusing explanation, but I
don't want to post the entire contents of my explanation of pointers
here, you'll want to read them with all the pretty pictures at
<http://www.zathras.de/howmemorymanagementworks.htm>. You may also
want to read the "Masters of the Void" C tutorial that's linked on
that page.
A pointer is simply a number, an address. An array is simply a
pointer, just that it points to a bunch of elements instead of one
(which you probably know, since you're using calloc() to create a new
block and return its address). It logically follows that an
NSRectArray is simply a variable containing a number (an address).
You do not want to change this number, so why are you passing a
pointer to this number into your function? Just pass in the address
by value. While it will copy the address, it will not copy the memory
block pointed to by this address, so you're effectively passing by
reference anyway.
Your problem above is probably one of precedence. What your code
does is get the index-th element of an array of NSRect*s, and then de-
reference that. While what you really want to do is first dereference
your NSRect** so you can then get the index-th element from it. To do
that, you'd probably want to say:
((*rowRects)[index]) = currentRowRect;
When in doubt, it's always a good idea to use brackets to explicitly
enforce execution order, instead of hoping the computer will do what
you intended it to do. :-)
Cheers,
-- M. Uli Kusterer
http://www.zathras.de
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden