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 20,000+ Swift developers and enthusiasts who get my weekly updates.