Shouldn't this be an ERROR?


I don’t understand how it is legal to instantiate multiple objects using the same pointer ex.(*person).

NSMutableArray *employees = [[NSMutableArray alloc] init];

for (int i = 0; i < 10; i++) {

        //  Instantiate Employee object
        Employee *person = [[Employee alloc] init];
        //  Give the instance variables interesting values
        //  Place employee in the employees array
        [employees addObject:person];


Also, how is it possible to add to a single array the same pointer multiple times.

Thank you in Advance. :smiley:


“[[Employee alloc] init]” reserved a block of memory at , let’s say, address 1001.

" *person = " assigned the result of the above method to the pointer variable “person”. The result of the method above is the address of the memory reserved. That address was 1001.

So variable “person” contains 1001.

Later in your code, the addobject method puts the value of “person” in the “employee” array. So address 1001 is now in slot 1 of the “employee” array.

The next time through the loop, you reserve (or allocate) another block at a new address, let’s say the new address is 1011. This address is assigned to the variable “person”, and the value of “person” is now 1011. The next addobject puts the current value of person (1011) into slot 2 of the “employee” array.

The answer to both your questions is the same: you’re using the same pointer variable but the value of the pointer variable is different each time.


Thanks for your time James. Thinking of the pointers as references to actual data instead of being the actual data has helped. Although I still seem to be confused with another part.

I’m finding it difficult to understand WHY Aaron would use the same pointer (*person) for all the different addresses that are all placed within the same Array (*employees). Wouldn’t it complicate things if you needed to send a message to a specific object in the Array and you didn’t know its index position (ex. “indexOfObject:”, “containsObject:”)? Under most circumstances I would have thought that having a unique pointer for each object in the same Array would be preferred.

I apologize for my lack of clarity. Thank you for your time. :smiley:


Your confusion, I believe, arises from the fact that arrays are exactly the solution you’re proposing whenever you don’t know in advance how many values you’re going to be working with (and even, often, if you do know).

If you know you’re expecting 3 values, sure, declare 3 different variables if they serve 3 different roles. Otherwise, put them in an array, and have enough identifying information in the objects to find them again later. For better or for worse, you can’t create new variables on the fly when you discover there are more values than you have programmed your code to deal with, unless, of course, those values are simply stored in an array (or hash, or set, or …).

I’ve noticed that some of the code in the chapters feels incomplete because objects don’t have enough identifying information. Stocks, for example, are completely anonymous, which makes them harder to conceptualize. It’s much easier to think about finding Apple’s stock in an array when the stock is identified with AAPL as a field that can be set and queried.


Thanks macintux! Storing data is always a tricky subject for me even more so when anticipating in which way it will be queried. Prior to this chapter I didn’t quite notice a lack of identifying properties for objects although after my confusion such details including pointers, scope and ownership have become muddled.

The concept of having the same pointer variable pointing to multiple addresses simultaneously has been really throwing me off. Although, after giving the chapter a tertiary reading I found that in the section “Adding a to-many relationship to Employee” of the book, the portion “Important things to know about collections and ownership” really helped lift some of the fog.

I was assuming that the collection was using the existing pointer and did not realize that the collection(array) was establishing(creating) a new pointer for each address that was added.