First for loop in chapter 19


My first post.

I have a little problem with the first for loop of chapter 19.
I understand how the loop generates 10 employee objects with different instance variable settings and that they’re added to the employees array. The difficulty I have is, how can each object have a pointer with the same name i.e. “person”, how would you access one of the instance variables of any of the employee objects i.e. employeeID with out accessing the array? For example if I insert the line

NSLog(@“person ID = %d”,[person employeeID]);

straight after the loop I have an error, which I would expect because how can the complier know which person I’m referring to.


The pointer person points to each new Employee object in turn. Then that Employee object is put into an array.

So, if you wanted to see the employeeID of a particular Employee outside the loop, you’d access it from the array:

[code]// Get the third employee in the array
Employee *favoriteLaborer = [employees objectAtIndex:2];

// Ask for its employeeID
NSLog(@“person ID = %d”, [favoriteLaborer employeeID]);[/code]


Thanks for the prompt reply.

So it’s not possible to access any one of the 10 employee objects other than through the array?
This still appears a little unclear to me as I had the understanding each pointer held a unique address, even if that pointer was being accessed via an array or not.

So if my new understanding were to be; once a pointer to an object is passed into an array the object receives a new unique address and the original object pointer address is disregarded, would this be correct?

Thanks again


I think reviewing the basics might help here:

When an object is allocated, it is created at some address and that address never changes for the life of the object.

A pointer (like person) can only hold one address at a time. When you put a new address in, it forgets the old address.

An array holds a list of addresses.


Great, it’s now making sense.
Thanks for you invaluable insight