I haven't found answer in any of the questions asked. I am passing an Integer Array to a function.Now I want to traverse through the array.But as things worked in C,C++ by simple using arrayname.length which gave the number of elements in array. What is the way to find that? [NSArrayObject length] works for NSArray type but I want it for int[] . not even [XYZ count] works....so want another way to find that out.
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.
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).
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]);
An array is basically just a memory address to the beginning of a block of memory that's been allocated for the array. To actually use it, you need to remember the length somehow after it's been created. You can either pass the length around to every function, or wrap both the array and length in another structure, passing that structure around.
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):
http://stackoverflow.com/questions/1281686/determine-size-of-dynamically-allocated-memory-in-c
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.