Why is my button click being called 4 times?

Probably a noob question, but I'm trying to write a simple iPhone app that increments a label by the number of button clicks. 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 is updated with a multiple of 4 (4,8,12,16, etc.). What could I be doing wrong here?

+2


source to share


1 answer


Look at the definition i

:

int *i = 0;

      



i

is not an integer - it is a pointer to an integer. The size of an int is 4 bytes in your architecture, so the pointer is incremented by 4 (which will be the address of the next int in the ints array). You want to declare it as simple as possible int i = 0

.

+10


source







All Articles