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
NSString *processName = [[NSProcessInfo processInfo] processName];
NSInteger *processID = [[NSProcessInfo processInfo] processIdentifier];
Which generated an error, after tinkeing around blindly, I discovered that it was the * in the NSInteger line that was causing the problem.
So I obviously don't understand what's happening. I'll explain how I think it works and perhaps someone would be kind enough to point out the flaw.
Unlike in web development, I now need to worry about memory, well, more so than in web development. So when I create a variable, it gets allocated a bit of memory somewhere (RAM I assume). Instead of passing the variable around, I pass a pointer to that bit of memory around. And pointers are declared by prefixing the variable name with *.
Assuming I'm right, what puzzles me is why don't I need to do that for NSInteger?
NSInteger
is a primitive type, which means it can be stored locally on the stack. You don't need to use a pointer to access it, but you can if you want to. The line:
NSInteger *processID = [[NSProcessInfo processInfo] processIdentifier];
returns an actual variable, not its address. To fix this, you need to remove the *
:
NSInteger processID = [[NSProcessInfo processInfo] processIdentifier];
You can have a pointer to an NSInteger
if you really want one:
NSInteger *pointerToProcessID = &processID;
The ampersand is the address of operator. It sets the pointer to the NSInteger
equal to the address of the variable in memory, rather than to the integer in the variable.
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.
The *
means “pointer”. The object variable holds a pointer to an object, so it has a *
; the NSInteger variable holds an NSInteger, not a pointer to an NSInteger, so it does not have a *
. Putting the *
on that variable gives you at least a warning because you're putting an integer into a pointer variable.
NSInteger is just a typedef for int, AFAIK.
Working with pointers
NSInteger integer1 = 1;
NSLog(@"1. integer1:%ld &integer1:%p", integer1, &integer1);
//1. integer1:1 &integer1:0x7ffee59e8a98
NSInteger *integer2 = &integer1;
NSLog(@"2. integer2:%p &integer2:%p *integer2:%ld", integer2, &integer2, *integer2);
//2. integer2:0x7ffee59e8a98 &integer2:0x7ffee59e8a90 *integer2:1
*integer2 = 2;
NSLog(@"3. integer2:%p &integer2:%p *integer2:%ld \t integer1:%ld &integer1:%p", integer2, &integer2, *integer2, integer1, &integer1);
//3. integer2:0x7ffee59e8a98 &integer2:0x7ffee59e8a90 *integer2:2 integer1:2 &integer1:0x7ffee59e8a98
来源:https://stackoverflow.com/questions/1014053/why-dont-i-declare-nsinteger-with-a