0

i'm trying to make a function that use % operator in objective c my function is:

-(void) image1:(UIImageView *)img1 
        image10:(UIImageView *)img10 
        image100:(UIImageView *)img100 
        score:(NSInteger *)sc
{
        NSInteger* s100,s10,s1;

    s100 = sc % 100;
    s10 = (sc - s100 * 100) % 10;
    s1 = (sc - sc % 10);
    NSLog(@"%d",s1);
}

but i got errors.. can you guide me to some solution

Mouhamad Lamaa
  • 884
  • 2
  • 12
  • 26

2 Answers2

5

NSInteger is a primitive type. Depending on the environment it is either typedef'd to a 32 bit or 64 bit signed integer.

s100's declaration is

NSInteger* s100

so s100 is a pointer. Taking the modulus of a pointer is wrong. That line should be:

NSInteger s100,s10,s1;

And sc should probably be an NSInteger too. If you do really want to pass a pointer to an NSInteger, you need to dereference it when you do arithmetic with it:

s100 = *sc % 100;
s10 = (*sc - s100 * 100) % 10;
s1 = (*sc - *sc % 10);
JeremyP
  • 84,577
  • 15
  • 123
  • 161
1

You can't implicitly create objects like that. You want s100, s10, s1 and sc all to be C-style variables, but you've declared s100 and sc as pointers, as though they were going to hold Objective-C objects. I suggest you change NSInteger* to NSInteger in both cases. Not all things starting in NS are objects.

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • You're completely right; I misread the source. I also missed that sc is a pointer. I'll correct my answer. – Tommy Dec 01 '10 at 11:09