Why I do not declare NSInteger with *

I tried my hand on a Stanford iPhone course on iTunes U and I got a little confused about the pointers. In the first assignment, I tried to do something like this

NSString *processName = [[NSProcessInfo processInfo] processName]; NSInteger *processID = [[NSProcessInfo processInfo] processIdentifier]; 

Having generated an error, after the blind blind, I found that it was in the NSInteger line that the problem arose.

Therefore, I obviously do not understand what is happening. I will explain how I think this works, and maybe someone will be kind enough to point out a flaw.

Unlike web development, I now need to worry about memory, well, moreover than in web development. Therefore, when I create a variable, it gets the allocation of bits of memory (RAM I assume). Instead of putting a variable around, I pass a pointer to this bit of memory around. And also pointers are declared by prefixing the variable name with *.

Assuming I'm right, which puzzles me, why don't I need to do this for NSInteger?

+42
pointers integer objective-c cocoa nsinteger
Jun 18 '09 at 17:29
source share
5 answers

NSInteger is a primitive type, which means that it can be stored locally on the stack . You do not need to use a pointer to access it, but you can if you want. Line:

 NSInteger *processID = [[NSProcessInfo processInfo] processIdentifier]; 

returns a valid variable, not its address. To fix this, you need to remove * :

 NSInteger processID = [[NSProcessInfo processInfo] processIdentifier]; 

You may have a pointer to NSInteger if you really want to:

 NSInteger *pointerToProcessID = &processID; 

Ampersand is the address of the operator. It sets a pointer to NSInteger equal to the address of the variable in memory, and not an integer in the variable.

+68
Jun 18 '09 at 17:32
source share

The reason you do not declare NSInteger with * is because it is not an object. NSInteger is just int or long :

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

If used in a 32-bit application, it is a 32-bit integer, and if it is embedded in a 64-bit application, it is a 64-bit integer.

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

On the other hand, objects can only be passed to other functions as pointers. This is due to the fact that objects have dynamic memory for them and therefore cannot be declared on the stack. Since int or long has a fixed amount of memory allocated for them, this is not a problem.

+25
Jun 18 '09 at 17:32
source share

* means "pointer". An object variable contains a pointer to an object, so it has * ; the NSInteger variable contains an NSInteger, not a pointer to an NSInteger, so it does not have * . Putting * on this variable gives you at least a warning, because you are putting an integer in a pointer variable.

+3
Jun 18 '09 at 17:54
source share

NSInteger is just a typedef for int, AFAIK.

+2
Jun 18 '09 at 17:33
source share

Work 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 
0
May 01 '19 at 15:35
source share



All Articles