473,836 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning an array?

I believe it is technically possible to return a pointer to the first
element of an array. I can persuade the returned pointer to act like an
array, with some qualifications. I'm specifically interested in
multidimensiona l arrays.

It is often said that arrays and pointers are virtually identical. My
observations are that my (gcc) compiler knows the difference between T*, T
a1[9], and a2[3][3].

What I'm currently trying to do is to return a pointer (or reference) to an
array which was passed in as a container for the results of matrix
calculations taking two other matrices as arguments. It's the same concept
as using ostream& print(&ostream out, const objType& obj){ out << obj.data;
return out; }. I don't absolutely /need/ the functionality, but it would
be more intuitive to work with in some instances.

AFAIK, there is no way to specify an array return type. Whereas I can
specify 'ostream&', I cannot specify 'T[][dimensions]' as a return type. I
can use 'T*' as the return type and cast the returned array to T*. But
then it's not the same type as was passed in. Not to mention that it is
rather tricky playing with arrays at that level.

There are various reasons I would rather not use the stl containers. I can
create my own containers. AAMOF, that's what I'm doing. I don't need nor
even want iterators. Is wrapping the array in some kind of class type
object the only viable approach to passing arrays around?
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05
19 1954
Steven T. Hatton wrote:
cout << "\n---------- matrix mat** -----------\n";
for(unsigned i = 0; i < size; i++)
{
if(i%rowSize == 0 && i != 0){cout<<"\n";}
cout << setw(4) << *(*(mat + i/size ) + i%size) <<" ";

the i/size and i%size can be replace with one i. These were carried over
from another form of the function that required them.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #11
Steven T. Hatton wrote:
[...]
BTW. I tried to figure out if it was meaningful to create an array of
references to the elements of another array. For example,
float m[3][3];
float& ref_m[3][3]; // somehow make ref_m[i][j] == m[j][i];

I concluded that this could not be done. Am I correct?


You cannot have an array of references. You can, however, have
a reference to an array (as you saw). You can wrap your reference
in a struct and have an array of that struct:

template<class T> struct ref {
T &d;
ref(T &d) : d(d) {}
};

The problem, however, is in initialising it. You would have to
intialise the elements of that array all manually:

float m[3][3];
ref<float> rm[3][3] = { { m[0][0], m[0],[1] ...

so I don't think it's a viable alternative.

Victor
Jul 22 '05 #12
Victor Bazarov wrote:
Steven T. Hatton wrote:
[...]
BTW. I tried to figure out if it was meaningful to create an array of
references to the elements of another array. For example,
float m[3][3];
float& ref_m[3][3]; // somehow make ref_m[i][j] == m[j][i];

I concluded that this could not be done. Am I correct?


You cannot have an array of references. You can, however, have
a reference to an array (as you saw). You can wrap your reference
in a struct and have an array of that struct:

template<class T> struct ref {
T &d;
ref(T &d) : d(d) {}
};

The problem, however, is in initialising it. You would have to
intialise the elements of that array all manually:

float m[3][3];
ref<float> rm[3][3] = { { m[0][0], m[0],[1] ...

so I don't think it's a viable alternative.

Victor


I don't believe that's any better than using an array of pointers. Unless
I'm missing something, you would have to access the data as rm[i][j].d
wouldn't you? What I was shooting for was something that acted just like
the array, but was indexed as it's transpose. For now, I don't have a use
for it because I took a different approach to solving the immediate
problem.

I do however have another question along these lines.

This works:

template < unsigned REMAINING, unsigned ORDER, typename T1, typename T2,
typename OP>
class Array2OpAssign {
public:
typedef T1 (&rank1T1)[ORDER];
typedef T1 (&rank1T2)[ORDER];

static rank1T1 result(rank1T1 a1, const rank1T2 a2) {
OP op;
a1[ORDER - REMAINING] = op(a1[ORDER - REMAINING], a2[ORDER - REMAINING]);
return Array2OpAssign< REMAINING - 1, ORDER, T1, T2, OP>::result(a1, a2);
}
};
However, this doesn't:

template <unsigned ORDER, typename T1, typename T2>
typedef T1 (&rank1T1)[ORDER]; // this ain't legal
typedef T1 (&rank1T2)[ORDER];
inline rank1T1 array2PlusAssig n( rank1T1 a1, const rank1T2 a2)
{
return Array2OpAssign< ORDER,ORDER, T1, T2, Plus<T1, T2> >::result(a1,
a2);
}

perhaps playing with typedefs on the calling end is the way to go. I don't
particularly like that idea. It's much nicer to have a clean convenience
function that invokes the static function on the Array2OpAssign. Any
suggestions on how to template that typedef for a function template return
type?

--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #13
JKop wrote:
You could, of course, try to work with _references_ to arrays, but the
syntax is really convoluted, to say the least.

I disagree. It's simple operator and operand precedence:
int &k[5]; //an array of 5 references to integers

int (&k)[5]; //a reference to an array of 5 integers
-JKop


template <unsigned ORDER, typename T1, typename T2 >
T1 (&array2PlusAss ign(T1(&t1)[ORDER], T2(&t2)[ORDER]))[ORDER]

That's f'ing psycho! Convoluted it far too kind!

Oh, and try setting the second parameter to const.

--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #14
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote...
Victor Bazarov wrote:
Steven T. Hatton wrote:
[...]
BTW. I tried to figure out if it was meaningful to create an array of
references to the elements of another array. For example,
float m[3][3];
float& ref_m[3][3]; // somehow make ref_m[i][j] == m[j][i];

I concluded that this could not be done. Am I correct?
You cannot have an array of references. You can, however, have
a reference to an array (as you saw). You can wrap your reference
in a struct and have an array of that struct:

template<class T> struct ref {
T &d;
ref(T &d) : d(d) {}
};

The problem, however, is in initialising it. You would have to
intialise the elements of that array all manually:

float m[3][3];
ref<float> rm[3][3] = { { m[0][0], m[0],[1] ...

so I don't think it's a viable alternative.

Victor


I don't believe that's any better than using an array of pointers. Unless
I'm missing something, you would have to access the data as rm[i][j].d
wouldn't you?


You could just add

operator T () const;
operator T& ();

to the struct and you don't need .d any more, AFAIUI.
What I was shooting for was something that acted just like
the array, but was indexed as it's transpose. For now, I don't have a use
for it because I took a different approach to solving the immediate
problem.
Whatever.

I do however have another question along these lines.

This works:

template < unsigned REMAINING, unsigned ORDER, typename T1, typename T2,
typename OP>
class Array2OpAssign {
public:
typedef T1 (&rank1T1)[ORDER];
typedef T1 (&rank1T2)[ORDER];

static rank1T1 result(rank1T1 a1, const rank1T2 a2) {
OP op;
a1[ORDER - REMAINING] = op(a1[ORDER - REMAINING], a2[ORDER -
REMAINING]);
return Array2OpAssign< REMAINING - 1, ORDER, T1, T2,
OP>::result(a1, a2);
}
};
However, this doesn't:

template <unsigned ORDER, typename T1, typename T2>
typedef T1 (&rank1T1)[ORDER]; // this ain't legal
Nonsense. Why ain't it legal?
typedef T1 (&rank1T2)[ORDER];
inline rank1T1 array2PlusAssig n( rank1T1 a1, const rank1T2 a2)
{
return Array2OpAssign< ORDER,ORDER, T1, T2, Plus<T1, T2> >::result(a1,
a2);
}

perhaps playing with typedefs on the calling end is the way to go. I
don't
particularly like that idea. It's much nicer to have a clean convenience
function that invokes the static function on the Array2OpAssign. Any
suggestions on how to template that typedef for a function template return
type?


I have no idea what you're talking about. This compiles just fine
(as it should, of course):

template<unsign ed U, typename T> class C {
typedef T (&Tarrref)[u];
public:
C();
void foo(Tarrref r);
};

int main() {
C<5,int> c;
int a[5];
c.foo(a);
}

Victor
Jul 22 '05 #15
Victor Bazarov wrote:
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote...
I don't believe that's any better than using an array of pointers. Unless
I'm missing something, you would have to access the data as rm[i][j].d
wouldn't you?


You could just add

operator T () const;
operator T& ();

to the struct and you don't need .d any more, AFAIUI.


I believe that will work. I need to give it a whack. I did something like
that for a similar situation last night and it worked. I had forgotten
about type conversion operators. Good call!
What I was shooting for was something that acted just like
the array, but was indexed as it's transpose. For now, I don't have a
use for it because I took a different approach to solving the immediate
problem.


Whatever.


But it /is/ nice to know I can do it. Now that I see that I probably /can/
make a transpose pointer matrix act like a normal matrix, that opens a lot
of possibilities. It's much easier to think about orthogonal
transformations and the like when I can use (almost) real transpose
(inverse) matrices rather than simulating them by dancing around with the
indeces.
template <unsigned ORDER, typename T1, typename T2>
typedef T1 (&rank1T1)[ORDER]; // this ain't legal


Nonsense. Why ain't it legal?


Look at it again.
typedef T1 (&rank1T2)[ORDER];
inline rank1T1 array2PlusAssig n( rank1T1 a1, const rank1T2 a2)
{
return Array2OpAssign< ORDER,ORDER, T1, T2, Plus<T1, T2>
>::result(a1,

a2);
}

perhaps playing with typedefs on the calling end is the way to go. I
don't
particularly like that idea. It's much nicer to have a clean convenience
function that invokes the static function on the Array2OpAssign. Any
suggestions on how to template that typedef for a function template
return type?


I have no idea what you're talking about. This compiles just fine
(as it should, of course):

template<unsign ed U, typename T> class C {
typedef T (&Tarrref)[u];
public:
C();
void foo(Tarrref r);
};


I tend to write:
template<unsign ed U, typename T>
class C {
/*...*/
};

It avoids confusion. ;)

--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #16
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote...
Victor Bazarov wrote:
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote... [...]
template <unsigned ORDER, typename T1, typename T2>
typedef T1 (&rank1T1)[ORDER]; // this ain't legal
Nonsense. Why ain't it legal?


Look at it again.


Stop playing games and state your opinion. If you have any, that is.
[..]

Jul 22 '05 #17
Victor Bazarov wrote:
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote...
Victor Bazarov wrote:
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote... [...] template <unsigned ORDER, typename T1, typename T2>
typedef T1 (&rank1T1)[ORDER]; // this ain't legal

Nonsense. Why ain't it legal?


Look at it again.


Stop playing games and state your opinion.

#include <iostream>
#include <string>

template <typename T>
typedef T Tfoo;
T foo(T& tf){return tf;}

int main()
{
std::string bar = "This is foo.\n";
std::cout << foo(bar);
}
//--------------EOF----------------
hattons@ljosalf r:~/code/c++/scratch/typedef/
Mon Oct 11 01:08:18:> g++ -ofoo main.cc
main.cc:5: error: template declaration of `typedef T Tfoo'
main.cc:6: error: `T' was not declared in this scope
main.cc:6: error: `tf' was not declared in this scope
main.cc:6: error: syntax error before `{' token
main.cc: In function `int main()':
main.cc:11: error: `foo' undeclared (first use this function)
main.cc:11: error: (Each undeclared identifier is reported only once for
each
function it appears in.)
hattons@ljosalf r:~/code/c++/scratch/typedef/
Mon Oct 11 01:08:19:>

#include <iostream>
#include <string>

template <typename T>
//typedef T Tfoo;
T foo(T& tf){return tf;}

int main()
{
std::string bar = "This is foo\n";
std::cout<< foo(bar);
}
//--------------EOF----------------

hattons@ljosalf r:~/code/c++/scratch/typedef/
Mon Oct 11 01:08:29:> g++ -ofoo main.cc
You have new mail in /var/spool/mail/hattons
hattons@ljosalf r:~/code/c++/scratch/typedef/
Mon Oct 11 01:15:04:>

It's a function template not a class template. You can put a typedef there.
If you have any, that is.

[..]


foo
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #18
Steven T. Hatton wrote:
[..]
template <unsigned ORDER, typename T1, typename T2 >
T1 (&array2PlusAss ign(T1(&t1)[ORDER], T2(&t2)[ORDER]))[ORDER]

That's f'ing psycho! Convoluted it far too kind!

Oh, and try setting the second parameter to const.


References themselves cannot be set to const because they are not
objects. You _can_ make 'T2' const:

template <unsigned ORDER, typename T1, typename T2 >
T1 (&a2PA(T1(&t1 )[ORDER], T2 const(&t2)[ORDER]))[ORDER]
.. ^^^^^

V
Jul 22 '05 #19
Steven T. Hatton wrote:
Victor Bazarov wrote:

"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote...
Victor Bazarov wrote:
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote...


[...]
>template <unsigned ORDER, typename T1, typename T2>
> typedef T1 (&rank1T1)[ORDER]; // this ain't legal

Nonsense. Why ain't it legal?

Look at it again.


Stop playing games and state your opinion.


It's a function template not a class template. You can put a typedef there.


I missed the fact that you decided to make it stand-alone function.
My impression was that you were talking about replacing the body of
the class definition with that other code. Apologies.

V
Jul 22 '05 #20

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

Similar topics

6
14041
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return value of a function acts like 'by Val' returning the value only (except for objects) can you make it return a reference instead? cheers, Krackers
7
7307
by: BrianJones | last post by:
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(...)
10
10302
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. Returning a pointer to the first element might do but I want to do what I've said. Fraser.
41
3836
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x = rand(); y = rand();
3
2706
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to return an array as a property because it will return a copy of the array instead a reference to it. How can I force the property to return a reference to the array? Is it only a feature of arrays? I hope normal class objects (including collections)...
5
19605
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
17
3271
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
13
2563
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns data from a query into an array. The intent is that the following code:
0
4094
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should return me Array element type code 1, but it is returning me type code 12 and the array in a junk or unreadable format . Our environment is JDK 1.4, ojdbc14.jar and Oracle 8i Not able to find out what they problem is and am not able to change the...
5
2683
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return that structure, is this valid? As shown in the code below I am allocating the structure in the function and then returning the structure. I know if the structure contained only simple types (int, float) this will work without problems as you...
0
10560
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 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...
1
10604
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,...
0
9389
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7796
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 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...
0
5659
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5831
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4466
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4026
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
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...

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.