473,406 Members | 2,705 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,406 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 1690
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.