0

Hello I have a loop and I have issues when int11=21 and when i=7.

-(NSString *)print01:(int)int11{
    int n =128;
    char array12[7];
    NSString *str;

if(int11==0)
 return str= [NSString stringWithFormat:@"00000000"];


    for(int j=0;j<8;j++)
        if ((int11-n)>=0){
             //When i=7 then int11=1 and n=1, the "i" here is 7
                      array12[j]='1'; 
              //and here is become 49!!!

            int11-=n;

        }
        else 
            array12[j]='0';

        n=n/2;
        NSLog(@"Last %d",j);
    }


   str= [NSString stringWithFormat:@"%s",array12];

    return str;
}
3
  • What i do you mean? There is only a j. Commented Feb 19, 2011 at 15:42
  • haha I am just testing. array12 is an array that holds zeros and ones as chars. j is for the loop. int11 is a parameter for print01 function and is integer. I have problem when int11 is 21 and when i=7. Commented Feb 19, 2011 at 15:42
  • @user622203 Testing the limits of your own sanity? :-) Glad to hear it's just a temporary thing, although I can't imagine why you wouldn't just use meaningful variable names all the time. Commented Feb 19, 2011 at 15:44

1 Answer 1

4

char array12[7]; and for(int j=0;j<8;j++) and array12[j]='1'; induce an out-of-bounds problem.

Sign up to request clarification or add additional context in comments.

2 Comments

what do you mean? array[0-7] and j=0-7. Where is the problem?
@user622203 array[7] has 7 "entries". With the index 0, 1, 2, 3, 4, 5, 6. You don't start to count with 0 in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.