Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the size of an array

i have some code that requires the use of a for loop to read variables from an array.

int size=sizeof names;
NSLog(@"thelast one is %d",size);
NSString *usersName=userName.text;
NSString *usersPass=passWord.text;
for (i=0; i<=size;i++){

    NSString *namesArray=[names objectAtIndex:i];
    NSString *passArray=[pass objectAtIndex:i];
    NSLog(@"namesArray %@",namesArray); 
    NSLog(@"passArray %@",passArray); 
    if([namesArray isEqualToString:usersName]){
        userValid=1;
    NSLog(@"The content of arry4 is %@",namesArray);


    }
    if([passArray isEqualToString:usersPass]){
        passValid=1;
        NSLog(@"The content of arry4 is %@",passArray);
    }

    else {
        userValid=0;
        passValid=0;
    }

}

I've been having some problems because every time this function is called from within the program, it's almost as if the 'sizeof names' is wrong, therefore not all values in the array are checked. I'm generally a Java programmer so i'm used to names.length, and i was told sizeof names is essentially the same thing... any help?

Cheers.

like image 399
gunmania0 Avatar asked Jun 16 '11 15:06

gunmania0


People also ask

How do I get the size of an array in C#?

To find the length of an array, use the Array. Length() method.

What is the size of an element in an array?

The theoretical maximum Java array size is 2,147,483,647 elements. To find the size of a Java array, query an array's length property. The Java array size is set permanently when the array is initialized. The size or length count of an array in Java includes both null and non-null characters.


2 Answers

Don't use sizeof. Use [names count].

like image 173
BJ Homer Avatar answered Oct 03 '22 07:10

BJ Homer


You want to use [names count] not sizeof names. Sizeof is going to give you the size of the actual names object pointer itself and not the number of elements, since it's dynamic memory type.

like image 34
Nektarios Avatar answered Oct 03 '22 07:10

Nektarios