Trying to understand the pointers


#1

I solved the challenge with the following code:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        NSString *item1 = @"rice";
        NSString *item2 = @"bean";
        NSString *item3 = @"sugar";
        NSString *item4 = @"coffe";
        NSMutableArray *grocery = [NSMutableArray array];
        [grocery addObject:item1];
        [grocery addObject:item2];
        [grocery addObject:item3];
        [grocery addObject:item4];
        
        for (NSString *g in grocery) {
            NSLog(@"Grocery`s item: %@", g);
        }
    }
    return 0;
}

Well, I made a diagram to understand what is going on with the pointers. They should be properly represented like this:

But, as I’ve seen in the book, to make things simpler, when writing a diagram we must represent it like this:

Did I get it correctly?


#2

Well, your first diagram is a little weird in two ways:
The grocery variable is not part of the array – it holds a pointer to the array.
Arrays are indexed starting at zero, so it is odd that you named the first one item1.


#3

Oops, you’re right. It’s safer to name the item according to the index number. As regards the grocery variable, maybe my diagram isn’t clear enough. I didn’t mean that grocery belongs to the array. Instead I was trying to show all the pointers declared in main, and how they are sometimes linked to others (notice that there’s no arrow pointing from the array to the grocery variable). But I think it was a bad idea.
Thank you for your reply. I’m enjoying the book.