swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Arrays as arguments ?


From: Dwight Wilson
Subject: Re: Arrays as arguments ?
Date: Tue, 1 Jul 1997 12:35:35 -0400 (EDT)

>I'm sure someone out there has already done this:
>
>I have an object which has an instance variable defined thus:
>
>short int myArr [MAX1] [MAX2];
>
>Given the limitations placed by ObjectiveC on the types of arguments that
>can be passed via messages, what is the best way to :
>
>a) pass an array like myArr as an argument to a message,
>b) pass such an array back as the return value of a message ?

Sven, 

There are several possibly approaches.

For maximum flexibility, I'd declare the method as taking an int **
and declare each row of the array dynamically.  Alternately, you could 
create an array myArrAux of int *, each element pointing to the start of a 
row in myArr that are pointers to the beginning of each row and pass that 
to the method.  The advantage of these methods is that your method will work
for 2D arrays of different dimensions.

If the method doesn't need to handle arrays of different dimensions,
you should be able to declare the parameter as an "int (*)[MAX2]", i.e., 
a pointer to an array of MAX2 ints.  Some people complain that pointers
to arrays make their brains hurt - they could use the kludge of wrapping
the array in a struct (making it the single public member of an ObjC
class) and passing a pointer to the struct/class.  

Dwight

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.
                  ==================================


reply via email to

[Prev in Thread] Current Thread [Next in Thread]