Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Getting a function to return an array

Reply
Thread Tools

Getting a function to return an array

 
 
Andrew Gentile
Guest
Posts: n/a
 
      01-01-2007
Hello,
I am trying to write a function which takes several floats as
inputs, then returns 1x6 array of floats. I normally program in
Matlab, and I am often confused by C. It seems that this should be an
easy task, but I can't seem to get it done. Can someone please
include a brief snippet of code which demonstrates how to get a
function to accept 2 floats as inputs, and returns an array of 6
floats.
Thanks.

Andrew

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      01-01-2007
Andrew Gentile a écrit :
> Hello,
> I am trying to write a function which takes several floats as
> inputs, then returns 1x6 array of floats. I normally program in
> Matlab, and I am often confused by C. It seems that this should be an
> easy task, but I can't seem to get it done. Can someone please
> include a brief snippet of code which demonstrates how to get a
> function to accept 2 floats as inputs, and returns an array of 6
> floats.
> Thanks.
>
> Andrew
>


You can't return arrays in C. This is a design decision that goes back
to the first versions of the language.

A work around is this:

typedef struct tagDataStruct {
double data[8192];
} DataStruct;

DataStruct Function(double inputdata)
{
DataStruct result;

// calculate the result .....

return result;
}
 
Reply With Quote
 
 
 
 
David T. Ashley
Guest
Posts: n/a
 
      01-02-2007
"Andrew Gentile" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hello,
> I am trying to write a function which takes several floats as
> inputs, then returns 1x6 array of floats. I normally program in
> Matlab, and I am often confused by C. It seems that this should be an
> easy task, but I can't seem to get it done. Can someone please
> include a brief snippet of code which demonstrates how to get a
> function to accept 2 floats as inputs, and returns an array of 6
> floats.


This is an interface design question, and there are a couple of ways of
doing it.

I hesitate to give this advice, as 'C' gives you more than enough rope to
hang yourself.

METHOD 1--ALLOCATE THE MEMORY IN THE CALLER

void caller(void)
{
float inputs[2];
float outputs[6];

.... assign the inputs, inputs[0] and inputs [1].

callee(inputs, outputs);

.... Here the outputs are assigned by the called function.
}

void callee(float *inputs, float *outputs)
{
Here you may use inputs[0] and inputs[1] (but no more).

Here you may assign outputs[0] through outputs[5] (but no more).
}

On second thought, I won't describe alternate methods.

In 'C', the brackets [] are an operator. Although the compiler may make
some optimizations, the brackets just say "index into this array, pointed
to, by a number of elements".

There really is no notion in 'C' of going beyond the limits of the array.
That has to be controlled carefully. Allocation information is not packaged
with the array.

In the example, above, addressing outputs[6] (beyond the end of the array)
is very likely to be a run-time disaster.

There are various "safer" ways to handle the above, but the above comes down
to the fact that the caller and callee have to have agreement about what is
being passed. They both need to agree that inputs is only 2 floats big and
outputs is only 6 floats big. The "unsafe" way to have that agreement is
implicit ... with the information not explicitly passed. The above code is
unsafe, but it should work for you as long as you don't go beyond the array
boundaries.

Also, inputs and outputs are passed with the "&" operator because 'C' has
the convention that arrays and pointers are the same thing. For other data
types, a & would be required.

Please post back with any specific questions.

Dave.


 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      01-02-2007
Andrew Gentile wrote:

> I am trying to write a function which takes several floats as inputs,
> then returns 1x6 array of floats. I normally program in Matlab
> and I am often confused by C. It seems that
> this should be an easy task, but I can't seem to get it done.
> Can someone please include a brief snippet of code
> which demonstrates how to get a function to accept 2 floats as inputs
> and returns an array of 6 floats.


You can't return an array in C
but you can return a pointer to the first element of the array.
But it seems that you are really more interested
in returning a vector (or matrix).
Take a look at The ANSI C Numerical Class Library

http://www.netwood.net/~edwin/svmtl/

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function doesn't return simply skip the return line hirsh.dan@gmail.com C++ 4 07-22-2008 09:55 AM
how do i pass an array to a function .. and also return an array back from the func???? bhejafry.mmk@gmail.com C Programming 3 11-30-2006 05:05 PM
How to pass 2D array to sub function and return 2D array? Davy Perl Misc 9 08-04-2006 07:46 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM



Advertisments