开发者

Length of an `int` array in Objective C

I am passing an Integer array to a function. Next I want to traverse through the array. In C and C++ this worked by开发者_Go百科 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.


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


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]);


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).


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.


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


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();
}


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.


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.


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜