6

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

4
  • 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. Commented Jul 8, 2011 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 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. Commented Jul 8, 2011 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: stackoverflow.com/questions/1003841/… Commented Jul 8, 2011 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 with its being posted. Commented Jul 8, 2011 at 22:16

1 Answer 1

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];
Sign up to request clarification or add additional context in comments.

3 Comments

Those aren't functions; they're methods.
@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.
But you made methods whose (pseudo-)names say that they're functions. Seems likely to cause confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.