6

Is it possible to pass an array as an argument for a function?

user559142
  • 12,279
  • 49
  • 116
  • 179
  • 5
    Sure, it's an overly simple question, but -5 in 7 minutes? What on earth? I've seen far worse (and more argumentative) questions last weeks and not hit that. – Steven Fisher Jul 08 '11 at 18:09
  • 1
    @Steven I can see how it can come across as harsh, but one of the principle tenets of a [good question](http://stackoverflow.com/questions/how-to-ask) is that you've done some research beforehand. This is the type of question that could *very* easily have been answered by spending a little time on Google or on some tutorials instead of knee-jerking on to SO. – Doug Stephen Jul 08 '11 at 18:28
  • 2
    Sure, but simple is supposed to be welcome here. Even if something's easily answered via a Google search, if the answer can be better it belongs here. This is now the second hit for this google query, and the first not directly tied to classic C arrays. See here: http://stackoverflow.com/questions/1003841/how-do-i-move-the-turtle-in-logo – Steven Fisher Jul 08 '11 at 21:21
  • @Steven: simple is allowed, thus the question is not closed. Lazy is not encouraged, thus it is downvoted. That question you linked to is among the most controversial in the history of SO. The other founder of SO [actually disagrees](http://meta.stackexchange.com/questions/31867/why-do-stupid-questions-and-their-answers-get-so-many-upvotes/31961#31961) with its being posted. – jscs Jul 08 '11 at 22:16

1 Answers1

20

Yes, of course.

C-array:

- (void)myFunction:(int*)array;
 ...

int bar[12];
[obj myFunction:bar];

NSArray:

- (void)myFunctionWithNSArray:(NSArray*)array;
...
NSArray *array = [[NSArray alloc] initWithObjects...];
[obj myFunctionWithNSArray:array];
Eimantas
  • 48,927
  • 17
  • 132
  • 168
Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
  • 2
    Those aren't functions; they're methods. – jscs Jul 08 '11 at 20:28
  • @Josh: Correct! At some point, the discussions stops being about Objective-C and rather about C, therefore I used methods as an example to stay within Objective-C. – Yann Ramin Jul 08 '11 at 23:52
  • But you made methods whose (pseudo-)names say that they're functions. Seems likely to cause confusion. – jscs Jul 09 '11 at 00:44