swarm-support
[Top][All Lists]
Advanced

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

Re: Vector passing


From: Paul E Johnson
Subject: Re: Vector passing
Date: Fri, 18 Aug 2000 08:33:25 -0500

address@hidden wrote:
> 
> Hi,
> how do I indicate in Swarm that a function returns a vector? I have tried
> several combinations in modelswarm.h and modelswarm.m - e.g., in
> modelSwarm.h:
> -(int[128]) vectorFunction   or
> -(int) vectorFunction[128]
> and the like, but none of them seems to work. Could you pls help me on that
> one?

I asked this one about 2 years ago.  It is a C question, really, and I
don't think most C manuals treat it very clearly.

The answer is that, in C, you cannot pass a vector as a return value.
You can pass back a pointer to the first value in the vector, as in

- (int *) vectorFunction
{
  int avector[128];
  blah blah;
  avector;  //same as  &avector[0]  , the address of the first argument
}  

That way, you call the function with
 int * thevector;
 thevector= [self vectorFunction];

An alternative,  which is preferred in some textbooks, is like this. I
think it is called pass by reference.

- (BOOL) vectorFunction: (int *) vec
{
        //do anything you want to the vector
        vec[32]= 9;
        return YES;  //just doing this to show you are not returning the
vector with "return"
}

In your calling code, you declare the vector, 

  int thevector[128];

and then pass its name as an argument to the function, as in

 [self vectorFunction: thevector];

Good luck.
 -- 
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700

                  ==================================
   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]