views:

76

answers:

1

Probably a noob question, but I'm trying to write a simple iPhone app that increments a label by the number of times a button is clicked. I have the following code:

#import "Controller.h"

int *i = 0;
@implementation Controller 
- (IBAction)buttonClicked:(id)sender {      
    NSString *numTimesClicked = [NSString stringWithFormat:@"%d",i++    ];     
    myLabel.text = numTimesClicked;     
}
@end

When I click the button, the label updates in multiples of 4 (4,8,12,16, etc). What might I be doing wrong here?

+10  A: 

Look at the definition of i:

int *i = 0;

i isn't an integer — it's a pointer to an integer. The size of an int is 4 bytes on your architecture, so the pointer increments by 4 (which would be the address of the next int in an array of ints). You want to declare it as just int i = 0.

Chuck
+1 Nice catch.
fbrereto
+1 and marked as answered. Thanks for the help... I'm coming from a c# background and I'm still getting my head around when to label something as a point and when not to.
Peter Walke