0

I have the following code which increments an existing value of a new variable:

-(NSString *)increaseId:(NSInteger *)config_id{

   NSLog(@"%d",config_id);//Show 0
   NSInteger *varfg = 0;//Init
   varfg = config_id +1;//In logic here the compiler do 0 + 1 = 1
   NSLog(@"%d",varfg);// Show 8 ??

   return varfg;
}

If the variable config_id get value '0', when I incremment + 1, the value is 8!, I wonder why this happened and how can I fix this?

Anindya Sengupta
  • 2,539
  • 2
  • 21
  • 27
user3372120
  • 134
  • 1
  • 10
  • Why are you using pointers to NSIntegers? An NSInteger isn't an object (i.e. it's not a subclass of NSObject). It's a simple typedef'd primitive (a long). – hsoi Mar 21 '14 at 01:04
  • What are you trying to do? – hsoi Mar 21 '14 at 01:09
  • 2
    Just taking a guess... if config_id is supposed to be a simple number, and then you're trying to increment the value by 1, then all you need is an NSInteger, not an NSInteger*. So change config_id to be a simple NSInteger, and varfg as well. – hsoi Mar 21 '14 at 01:10
  • Well, everything I need and pick up this coming integer value of this parameter, and perform a check if it is odd or even, this part is not put in the code because it was not necessary, but the idea is the same, so that the scan does not work because it increments this nonsense value – user3372120 Mar 21 '14 at 01:11
  • That's right, because by using NSInteger* you're working with a pointer and incrementing the pointer -- not the value the pointer is pointing to. So... it comes down to what you're trying to do: work with this value or with a pointer to the value. We've given you some insight, so it just depends upon the rest of your surrounding code context as to what your specific solution will be. – hsoi Mar 21 '14 at 01:15
  • Well its orks thanks! but wait a moment? what is the difererence of put * and not put? – user3372120 Mar 21 '14 at 01:16
  • That star makes it a pointer. – Chuck Mar 21 '14 at 01:18
  • OK I understand, but where did that crazy value? – user3372120 Mar 21 '14 at 01:23
  • 1
    That "crazy value" is the address where the next item in the array would be if `varfg` pointed to an array of NSIntegers. That's what you get when you add to a pointer. – Chuck Mar 21 '14 at 01:42

2 Answers2

2
- (NSString *)increaseId:(NSInteger)config_id
{

   NSLog(@"%d",config_id);
   NSInteger varfg = config_id + 1;
   NSLog(@"%d",varfg);// Show 8 ??

   return [NSString stringWithFormat:@"%d", varfg];
}

The pointer-to-integer doesn't make sense in your original (it could make sense in some contexts but it's clear from your question that this isn't one of them). So use a plain NSInteger, which isn't an object, just a way of declaring a primitive integer.


The reason that the value "8" appeared is a side effect of a phenomenon known as pointer arithmetic. In the line varfg = config_id +1; what has happened is that you are "adding one" to a pointer to an integer. That pointer value starts out at location 0 (because you set it there), but the size of an NSInteger on a 64-bit machine is 64 bits, or 8 bytes. The addition increments the pointer by one "NSInteger sized amount of memory", which thus gives it a value of 8.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
0

You are passing a pointer to an integer, so you have to dereference it...

NSLog(@"%d", *config_id);

If you really want to pas an integer, you should have this...

-(NSString *)increaseId:(NSInteger)config_id;

Also, your signature says you are returning NSString* but you are not doing that in your code.

SO, if you just want to pass the integer...

-(NSString *)increaseId:(NSInteger)config_id{
    NSLog(@"%d", config_id);//Show 0
    NSInteger varfg = 0;//Init
    varfg = config_id +1;//In logic here the compiler do 0 + 1 = 1
    NSLog(@"%d",varfg);// Show 8 ??

    return [NSString stringWithFormat:@"%d", varfg];
}
Jody Hagins
  • 27,943
  • 6
  • 58
  • 87