Why don't I declare NSInteger with a *

前端 未结 5 1028
孤城傲影
孤城傲影 2020-12-08 06:34

I\'m trying my hand at the iPhone course from Stanford on iTunes U and I\'m a bit confused about pointers. In the first assignment, I tried doing something like this

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 07:23

    The reason that you don't declare NSInteger with a * is because it isn't an object. An NSInteger is simply an int or a long:

    #if __LP64__
    typedef long NSInteger;
    #else
    typedef int NSInteger;
    endif
    

    If it's being used in a 32-bit application, it's a 32-bit integer, and if it's being built in a 64-bit application, it's a 64-bit integer.

    Of course, you can pass an NSInteger as a pointer, but most functions simply take arguments as an NSInteger and not a pointer to it.

    Objects, on the other hand, can only be passed to other functions as pointers. This is because objects have memory dynamically allocated for them, and so cannot be declared on the stack. Since an int or long has a fixed amount of memory allocated for them, this is not an issue.

提交回复
热议问题