Length of an `int` array in Objective C

Viewed 87072

I am passing an Integer array to a function. Next I want to traverse through the array. In C and C++ this worked by simply using arrayname.length which gave the number of elements in array. What is the way to find that in Objective-C?

[NSArrayObject length] works for NSArray type but I want it for int[]. Not even [XYZ count] works (an already suggested answer) so I'm looking for another way here.

10 Answers

this code can be use when the total number of element in array are not known.

main()
{
    int a[]={1,2,3,4,5,6,7}
    int i;
    clrscr();
    for (i=0;i<=((sizeof(a)/sizeof(int));i++)
    {
        puts(a[i]);
    }
    getch();
}

You should try the following approach:

float ptArray[] = {97.8, 92.3, 89.4, 85.7, 81.0, 73.4, 53.0, 42.3, 29.4, 14.1};

int x = sizeof(ptArray)/sizeof(ptArray[0]);

 NSLog(@"array count = %d", x);

Output:

array count = 10
Related