Pointers, Pointers, Everywhere!

To learn iOS programming, I’m starting with the very basics: by learning C and Objective-C from the Big Nerd Ranch Objective-C Guide. Most of what I’ve learned so far (I’m on Chapter 8) is pretty basic stuff: strings / integers / floats / functions. But today, I learned something new and interesting: Pointers!

Try this out in Xcode:

#include <stdio.h>

int main(int argc, const char * argv[])
{
    // declare variable i
    int i = 17;

    // store the location in memory of variable i
    int *addressOfI = &i;

    // addressOfI is the location in memory
    printf("i stores its value at %p\n", addressOfI);
    // prints "i stores its value at 0x7fff5fbff83c"

    // you can get the location of any variable by putting an & in front of it
    printf("i stores its value at %p\n", &i);
    // prints "i stores its value at 0x7fff5fbff83c"

    // you can get the start location of a function
    printf("this function starts at %p\n", main);
    // prints "this function starts at 0x100000e70"

    // you can get actual value of a variable at a specific location
    printf("int stored at addressOfI is %d\n", *addressOfI);
    // prints "the int stored at addressOfI is 17"

    // you can assign a new value to a location!
    *addressOfI = 89;
    printf("Now i is %d\n", i)
    //prints "Now i is 89"

    return 0;
}

I’m going to guess this pointer stuff will be pretty crucial for memory management in iOS programming. Excited to learn more!

Enjoy the article? Join over 14,500+ Swift developers and enthusiasts who get my weekly updates.

  • Arnaud Bertrand

    Why didn’t you get cs106b? You’d learn a lot about pointers. The main use is to manipulate data stored in the heap. A little bit tricky but amazingly powerful.

    • Arnaud,

      CS106B starts by having you build an entire calculator app without any basics. It was overwhelming and confusing, so I just gave up and learned ruby instead. In contrast, the Big Nerd Ranch book starts with the very basics and builds up, which is working A LOT better for me.

      • Arnaud Bertrand

        Well, the version I followed started with a transition from Java to C++, then recursion, then pointer and “linked list”, then sorts, graph and finally a “BASIC” interpreter (probably close to what you call the calculator).

        If the start was this last exercise, well, you have good reason to be confused.

        Personally I found this course harder than expected, but I found precisely what I was looking for: to go beyond the language, and advanced development techniques.

        Maybe you should have a look at it now, and you could find something interesting (I read a couple of your posts and I’m sure you are ready).

        Anyway, I’m happy that you found something new to discover with iOS apps development.

        • Oh, I thought you were talking about C193p, the one with iPhone app development! I never did CS106B, mostly because I was anxious to build stuff. I just skipped to web development with Ruby on Rails, and am now learning iOS.