473,320 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Getting a function to return an array

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

Jan 1 '07 #1
3 15622
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;
}
Jan 1 '07 #2
"Andrew Gentile" <an************@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr 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.
Jan 2 '07 #3
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 =----
Jan 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tek9_AK | last post by:
I need to find a way to transfer all the values of an array inside a function out of the fuction into another array. IE function splice($filename){ if(file_exists($filename)){...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
1
by: Andrew Poulos | last post by:
Say I have a multi-dimensional array and I have an index (held in a variable) that is itself an array, how do I get the array element that is referred to. For example, myArray = ], 5]; ind = ;...
21
by: Gerry Abbott | last post by:
Hi All, If completed a script which parses a string into fragments (fields), then assigns these substrings into an array. I wish to turn this into a function to which i can pass the string....
11
by: The Crow | last post by:
i have a arraylist. say it contains integer values. i want to be able to inform user, which indexes in the array contain same values. but there can be N different values, and M different indexes...
1
by: kamleshsharmadts | last post by:
I am using Ajax with struts in web application. from jsp i am calling a function of ajax.js onclick of a button. code of that call function which calling from jsp given as below:- ...
3
by: rich | last post by:
I keep getting the error: Warning: in_array(): Wrong datatype for second argument on line 679 here is the code. I declare and fill the array $specprimA = array(); $specprimA=...
1
by: simbarashe | last post by:
Hie could someone please help me with getting and using the current page url. I have a function that gets the url, I want to use it with header(location : XXX) but it wont work. The code is as...
8
by: BoneIdol | last post by:
Anyway to do it? I know you can use a variable's contents as a variable name with $$name. With something like this: <?php function foo($bar) { return $bar; } $name = foo($variable_name);
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.