473,382 Members | 1,441 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,382 software developers and data experts.

Is it possible to return an array from a function?

It is ok to return a struct variable from a function. How about an
array of any type from a function?

Because the array is allocated in the stack, it is no meaning to return
it, which ever type it is. This array is the memory of stack.
But for a struct, it is allocated

Nov 14 '05 #1
2 1689
chming wrote:
It is ok to return a struct variable from a function. How about an
array of any type from a function?

Because the array is allocated in the stack, it is no meaning to return
it, which ever type it is. This array is the memory of stack.
But for a struct, it is allocated

Straight from the Standard:

6.7.5.3 Function declarators (including prototypes)

Constraints

[#1] A function declarator shall not specify a return type
that is a function type or an array type.

-- and --

6.9.1 Function definitions

[#3] The return type of a function shall be void or an
object type other than array type.

So in other words, no. You can, however, return blocks of memory
allocated using malloc() and friends. So if you wish to return the
contents of an array you may do so indirectly by copying it into a
buffer allocated with malloc() and returning its address.

--John
Nov 14 '05 #2

"John Valko" <no*@available.info> wrote in message
news:cs**********@mailhub227.itcs.purdue.edu...
chming wrote: <snip>
So if you wish to return the contents of an array you may do so indirectly by copying it into a buffer allocated with malloc() and returning its address.


Of simply return a pointer to the array. Mind the scope of the array,
though.
Nov 14 '05 #3

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

Similar topics

2
by: burdeen | last post by:
I've been trying to return an array with PHP5 soap. Is this not supported? or am i doing it wrong? Seems to return on the last element in the array. In my WSDL i've defined my return type as a...
20
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then...
3
by: Ramprasad A Padmanabhan | last post by:
Suppose I have a function that expects an array as argument { ..... int d = { 1,2,3,4} someFunc(d); } I have to create a temporary array d and pass 'd' to the function. Can I avoid the the...
1
by: chming | last post by:
It is ok to return a struct variable from a function. How about an array of any type from a function? Because the array is allocated in the stack, it is no meaning to return it, which ever type...
13
by: Alison Givens | last post by:
....... that nobody knows the answer. I can't imagine that I am the only one that uses parameters in CR. So, my question again: I have the following problem. (VB.NET 2003 with CR) I have a...
5
by: JB | last post by:
I am struggling to figure out a way to allow one element to be dragged, but still capture 'mouseover' events on other elements. I've created a simple example to demonstrate what I mean:...
4
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
9
by: Miro | last post by:
VB 2003 at the end of the code, this works great. bytCommand = Encoding.ASCII.GetBytes("testing hello send text") udpClient.Send(bytCommand, bytCommand.Length) and this recieves it Dim...
8
by: Giovanni R. | last post by:
Take a look at this code (you can execute it): error_reporting(E_ALL); function byVal( $v) {} function byRef(&$v) {} print '<pre>'; byVal ($first); // gives a notice
1
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.