Hi, if you have a function, how is it possible to return an array? E.g.:
unsigned long[] function(...) // what I want to do, obviously illegal
I do know such would be possible by using a dynamic array e.g:
array *a;
a = function(...)
where function proto is
unsigned long * function(...) // etc
But I want to use static arrays instead. If returning a static array is not
possible, is it possible to pass one by reference, something like:
void function(&array ,...)//etc
Thanks,
Ben 7 7300
Problem solved. What a stupid mistake I'd made!
- Ben -
"BrianJones " <br***@jones161 1.fsnet.co.uk> wrote in message
news:cd******** **@newsg4.svr.p ol.co.uk... Hi, if you have a function, how is it possible to return an array? E.g.:
unsigned long[] function(...) // what I want to do, obviously illegal
I do know such would be possible by using a dynamic array e.g:
array *a;
a = function(...)
where function proto is
unsigned long * function(...) // etc
But I want to use static arrays instead. If returning a static array is
not possible, is it possible to pass one by reference, something like:
void function(&array ,...)//etc
Thanks, Ben
"BrianJones " <br***@jones161 1.fsnet.co.uk> schreef in bericht
news:cd******** **@newsg1.svr.p ol.co.uk... Problem solved. What a stupid mistake I'd made! - Ben -
"BrianJones " <br***@jones161 1.fsnet.co.uk> wrote in message news:cd******** **@newsg4.svr.p ol.co.uk... Hi, if you have a function, how is it possible to return an array? E.g.:
unsigned long[] function(...) // what I want to do, obviously illegal
I do know such would be possible by using a dynamic array e.g:
array *a;
a = function(...)
where function proto is
unsigned long * function(...) // etc
But I want to use static arrays instead. If returning a static array is not possible, is it possible to pass one by reference, something like:
void function(&array ,...)//etc
Thanks, Ben
Use std::vector
"BrianJones " <br***@jones161 1.fsnet.co.uk> wrote... Hi, if you have a function, how is it possible to return an array?
It is not possible. But read on.
E.g.:
unsigned long[] function(...) // what I want to do, obviously illegal
Yes, it is illegal. Arrays are special objects. They don't have copy
semantics defined for them, and copy semantics are required for return
values.
I do know such would be possible by using a dynamic array e.g:
array *a;
a = function(...)
where function proto is
unsigned long * function(...) // etc
That's not a dynamic array. It's a pointer. It could _point_ to the
first element of a dynamic array, yes. But that's just a convention
and not the real way of "returning" an array.
A dynamic array is still an array, which doesn't have many things defined
for it unlike single objects (and pointers).
But I want to use static arrays instead. If returning a static array is
not possible, is it possible to pass one by reference, something like:
void function(&array ,...)//etc
Yes, it's possible. But the example is a syntax error. To declare your
function to accept the first argument as a reference to an array, you
need to write
void function(type (& argumentname)[size])
where 'size' has to be a constant expression, 'type' has to be the type
of a single element of your array, and 'argumentname' is the name of
the argument (which can be omitted in a declaration:
void function(type (&)[size]);
but has to be present in a definition if you intend to use the argument
inside the function).
Just like a reference to an array, you may pass a pointer to an array:
void function(type (* argumentname)[size])
HTH
Victor
BrianJones posted: Hi, if you have a function, how is it possible to return
an array? E.g.:
unsigned long[] function(...) // what I want to do,
obviously illegal I do know such would be possible by using a dynamic array
e.g: array *a;
a = function(...)
where function proto is
unsigned long * function(...) // etc
But I want to use static arrays instead. If returning a
static array is not possible, is it possible to pass one by reference,
something like: void function(&array ,...)//etc
Thanks, Ben
You've to specify the array size:
unsigned long[15] Blah();
int main()
{
const (&unsigned long)[15] = Blah();
}
If you don't specify the array size, then you'll need
pointers.
-JKop
> unsigned long[15] Blah(); int main() { const (&unsigned long)[15] = Blah(); }
CORRECTION
unsigned long[15] Blah()
{
unsigned long blah[15] = {1,2,3,4,5,6,7, 8,9,10,11,12,13 ,14,15};
return blah;
}
int main()
{
const unsigned long (&blah)[15] = Blah();
extern void SomeFunc(const unsigned long (&)[15]);
SomeFunc(blah);
}
-JKop
JKop <NU**@NULL.NULL > wrote: CORRECTION
unsigned long[15] Blah() {
Syntax error
Even if you meant:
typedef unsigned long ulong_15[15];
ulong_15 Blah();
a compiler error would be required, since arrays cannot be passed by value.
unsigned long blah[15] = {1,2,3,4,5,6,7, 8,9,10,11,12,13 ,14,15};
return blah;
The name of an array decays to a pointer to its first element. So even
if this worked, you have returned a pointer to an object that no
longer exists. This is why the OP asked about static arrays.
BrianJones wrote: if you have a function, how is it possible to return an array? E.g.:
unsigned long[] function(...) // what I want to do, obviously illegal
What about boost::array from http://www.boost.org/doc/html/array.html ?
#include <boost/array.hpp>
using boost::array;
const std::size_t N = 4;
array<unsigned long, N> function(...)
{
array<unsigned long, N> result = { { 0, 1, 2, 3 } };
return result;
}
Regards,
Niels Dekker www.xs4all.nl/~nd/dekkerware This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Justin Naidl |
last post by:
class Foo
{
protected:
char foo_stuff;
public:
char* get_foo_stuff();
}
Given the above example. What I want to know is the "proper/standard" way
|
by: Tom |
last post by:
Shouldn't you be able to return them as 3 By Ref arguments
in the function call?
>-----Original Message-----
>I have a class with a function which need to return to
the caller three
>two-dimensional arrays. Right now the function sends the
arrays as an array
>of arrays. The caller then decomposes the array of arrays
back to its
|
by: Robert Smith |
last post by:
I am wondering why it is possible to return a pointer to a string literal
(ie. 1) but not an array that has been explicitly allocated. (ie. 2) ?
Both would be allocated on the stack, why does the first one not cause a
compiler warning?
#include <stdio.h>
char * funca() {
char *a = "blah"; //1 - ok
// char a = "blah"; //2 - not ok
|
by: jodleren |
last post by:
Hi!
I have a function, a part of my code which I can use as a function. It
will return 2 arrays, and I am wondering what way to do so. Both
arrays hold strings, there are no special keys.
1) setting the arrays as globals
2) returnin an array of arrays
3) returning a large array with a known marker to indicate when the
2nd part starts.
|
by: Jim |
last post by:
Hello,
I have a very simple object, freqoffset which I'm trying to use as
members of a static array of in another class
class freqoffset
{
public:
freqoffset():offset(0),strength(0){};
int set(double offset,double strength);
double getOff() const{return offset;};
double getStr() const{return strength;};
| |
by: Jim |
last post by:
Hi,
I'm trying to build a simple data table for all my classes to use. I
don't want to use a vector. The data is stored in class freqoffset
class freqoffset
{
public:
freqoffset():offset(0),strength(0){};
int set(double offset,double strength);
double getOff() const{return offset;};
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |