0

So I'm used to developing in Java and Swift, where pointers aren't really something I have to worry about.

But I find myself on a project involving some Objective-C code that I can't quite get.

I understand putting a * before a name declares it as a pointer, and I've seen some instances where declaring it after can also be used as a pointer.

So imagine my frustration when I see interface methods I'm expected to implement whose signatures include int * and int ** arguments, and the description I'm given is that these are arrays (and ** is a 2d array).

Google tells me it's a pointer (and a pointer to a pointer), but that doesn't make sense to me since a pointer to me implies one variable in memory that you are pointing to, not a collection of them.

So how does this work, and how can I iterate over them?

jscs
  • 63,694
  • 13
  • 151
  • 195
Joey Nash
  • 35
  • 6
  • 3
    I can't give a high-quality answer at the moment, but it might be helpful to learn about [C arrays](https://www.cs.swarthmore.edu/~newhall/unixhelp/C_arrays.html); in C (and thus in Objective-C), arrays of values lay out those values sequentially in memory, one after the other. The start of the array can be referenced using a pointer, and through that pointer, accessing subsequent elements in the array is a bit of trivial math. So an `int *` can point to _one_ `int` in memory, or a whole list of them laid out sequentially (and just be pointing to the first one in the list). – Itai Ferber Mar 13 '18 at 19:44
  • Ok, makes sense. The 2D one would likely point to one of several pointers that each point to a collection of ints. So then I'm left wondering how to access/enumerate over them, and in the first place how to tell the difference between one pointing to one int and one pointing to a list. – Joey Nash Mar 13 '18 at 19:46
  • @JoeyNash Pointers to lists of elements are accessed exactly like arrays; `ptr[0]` to get the first `int`, `ptr[1]` to get the second one, etc. So the easiest way to enumerate is a `for` loop. You can also use pointer arithmetic by repeatedly adding 1 to the pointer and dereferencing, which might perform slightly better, although it can be more confusing. – Charles Srstka Mar 13 '18 at 19:55
  • @JoeyNash As for how to tell the difference, the only way is to read the documentation for the API you're using; additionally, make sure you know how many elements there are supposed to be, because if you try to read past that—for example, `ptr[4]` when there are less than 5 elements—C will happily let you do it, and you'll read whatever random memory comes after the list. Welcome to the wild and wooly world of C. – Charles Srstka Mar 13 '18 at 19:57
  • There's a fair number of C questions on this, e.g. https://stackoverflow.com/questions/7586702/is-2d-array-a-double-pointer, but the fact that this is a method parameter is also an important piece. It's equivalent to it being the parameter of a C function, however, so you can also look for "2d arrays as C function paramters". Really, as Charles and Itai have pointed out, this is entirely a C question, but as soon as you mention ObjC the C folks here take the [c] tag off. :) – jscs Mar 13 '18 at 20:32

0 Answers0