39

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.

TT--
  • 2,956
  • 1
  • 27
  • 46
Darpan
  • 393
  • 1
  • 3
  • 4

10 Answers10

31

You can use [XYZ count] to get the length of the array

user2070420
  • 533
  • 6
  • 12
26

There isn't anything specific to Objective-C with an array of ints. You would use the same technique as if you were using C.

sz = (sizeof foo) / (sizeof foo[0]);
mouviciel
  • 66,855
  • 13
  • 106
  • 140
Andy Gaskell
  • 31,495
  • 6
  • 74
  • 83
  • I tried this...i saw dis for a 2-dimension array but my array is single dimension...like just int foo[] so wen i do sizeof(foo)/sizeof(foo[0]) it returns 4/4 i.e. 1 ..i have already tried it...but no luck..is there any other way? Thanks – Darpan Oct 07 '09 at 07:32
  • 3
    @Darpan: This works only when you declare int foo[5], not int foo[]. – mouviciel Oct 07 '09 at 07:42
  • agree w/ mouviciel, this won't get what you want. – pxl Oct 07 '09 at 15:48
  • i have experimented on this. the correct way to display int foo[] length is use sizeof (foo)/4 . need to divide by 4. it works also to not primitive (NSString). – HelmiB Jul 08 '11 at 03:47
21

There is no such thing as array.length in C. An int array in Objective-C is exactly the same as an int array in C. If it's statically defined like int foo[5], then you can do sizeof(foo) to get the size — but only in the same function foo is defined in (to other functions, it's just an int pointer, not an array per se). Otherwise, there is no inherent way to get this information. You need to either pass the size around or use sentinel values (like the terminating '\0' in C strings, which are just arrays of char).

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • I am looking for length i.e. number of elements in an integer array. and not sizeof.as wen id o sizeof it simply returns me the sizeof int – Darpan Oct 07 '09 at 07:29
  • 5
    As I said, no can do. `sizeof()` works for statically sized arrays like the example I gave above in the same function they are defined in. That's the closest C comes to supporting this. – Chuck Oct 07 '09 at 07:40
8

[array count] this appears to work the easiest in objective-c

chillininvt
  • 107
  • 1
  • 1
6

Huh? In C, there's no such thing as "arrayname.length". An array of a primitive type, such as int[], does not have any length information associated with it at runtime.

unwind
  • 391,730
  • 64
  • 469
  • 606
3

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();
}
Matthew Frederick
  • 22,245
  • 10
  • 71
  • 97
  • 2
    4 spaces before a line of text tells the site to use code formatting. To quickly put 4 spaces before many lines of code, select all of the lines and click the **{}** button. I fixed it for you, to make it readable. – Matthew Frederick Jan 09 '11 at 08:15
2

There is no such thing as arrayname.length in C. That is why so many functions take argument pairs, one argument with the array and one argument with the length of the array. The most obvious case for this is the main function. You can find this function is all your iPhone projects in a file named main.m, it will look something like this:

int main(int argc, char *argv[]) {    
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  int retVal = UIApplicationMain(argc, argv, nil, nil);
  [pool release];
  return retVal;
}

Notice how the argv agument is a vector, or array of strings, and the argc argument is a count of how many items that are in this array.

You will have to do the same thing for all primitive types, this is the C part of Objective-C. If you stay in the Objective parts using NSArray or subclasses works fine, but requires all elements to be objects.

PeyloW
  • 36,742
  • 12
  • 80
  • 99
1

looks like a job for NSMutableArray. is there a reason why you need to work with a C array? if not, consider NSMutableArray.

i may be wrong (so please correct me if i am), but there's no easy way in C to get the size of int[] at runtime. you would have to do something like create a struct to hold your array and an int where you can keep track of how many items in your array yourself.

even fancier, you can make your struct hold your array and a block. then you can design the block to do the tracking for you.

but this is more a C question, not an objective-c quesiton.

pxl
  • 1,297
  • 10
  • 16
1

If you want an array of integers, wrap them in NSNumber objects and place them into an NSArray.

What you are asking to do, is not really supported well in C though there is a method on the mac you could try (malloc_size):

determine size of dynamically allocated memory in c

Community
  • 1
  • 1
Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
0

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
Gonzalo Garcia
  • 6,192
  • 2
  • 29
  • 32
RichApple
  • 7
  • 2