views:

3591

answers:

7

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.

+2  A: 

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
+10  A: 

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
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
Just use sizeof(foo)/sizeof(foo[0])
mouviciel
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
+2  A: 

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]);
Andy Gaskell
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
@Darpan: This works only when you declare int foo[5], not int foo[].
mouviciel
agree w/ mouviciel, this won't get what you want.
pxl
+3  A: 

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.

James M.
+1  A: 

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
A: 

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

Kendall Helmstetter Gelner
+1  A: 

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