473,399 Members | 4,254 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,399 software developers and data experts.

Passing array by reference

This might be a really simple question, but here goes: How can I pass an
array to a function and have the function put values into that array? Some
code to clearify (mat is a matrix class)

float m[16];
mat = matX * matZ * matT;
mat.getElements( m );
Render.setMeshTransform( m );

In my matrix class I have

void getElements( TYPE tMatrix[] ) {
tMatrix = m_tElements;
}

float m_tElements[16];

This, of course, works one way; getElements can access the values in
tMatrix. But when the function returns, m is not filled with the values from
m_tElements. So, is it possible to somehow pass an array by reference,
(without new'ing it!)?


Jul 19 '05 #1
2 33995

"news.tiscali.dk" <bu**@whitehouse.gov> wrote in message news:DO*********************@news010.worldonline.d k...
This might be a really simple question, but here goes: How can I pass an
array to a function and have the function put values into that array? Some
code to clearify (mat is a matrix class)

Arrays are always passed by reference (this is a goofy inconsistency that's
been in C since the dawn of time). Well, what really happens is that the thing
that looks like an array declaration in your getElements function really gets
converted to a TYPE* tMatrix. So you're passing a pointer to the first float
in the array. This isn't your problem.

The real problem you are running into, is that arrays are also inconsistant
in that they can't be assigned.
void getElements( TYPE tMatrix[] ) {
tMatrix = m_tElements;
}


You'll have to copy the elements individually:
for(int i = 0; i < 16; ++i) tMatrix[i] = m_tElements[i];
for example.

However, what's really easier is to use vector. vector is an array like
object that behaves normally (unlike arrays). If you want to pass it by
reference, you can use the C++ reference syntax.

#include <vector>
using std::vector;

vector<float> m(16);
mat.GetElements(m);

vector<float> m_tElements(16);
void getElements(vector<TYPE>& tmatrix) {
tmatrix = m_tElements;
}

Jul 19 '05 #2
> However, what's really easier is to use vector. vector is an array like
object that behaves normally (unlike arrays). If you want to pass it by
reference, you can use the C++ reference syntax.

#include <vector>
using std::vector;

vector<float> m(16);
mat.GetElements(m);

vector<float> m_tElements(16);
void getElements(vector<TYPE>& tmatrix) {
tmatrix = m_tElements;
}


Op should also check out "vector::swap()" which many programmers don't seem
to be aware of (not referring to you personally :). It's much faster than
direct assignment where applicable.
Jul 19 '05 #3

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

Similar topics

15
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. ...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: DaTurk | last post by:
If I call this method, and pass it a byte by ref, and initialize another byte array, set the original equal to it, and then null the reference, why is the original byte array not null as well? I...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
5
by: aelred | last post by:
I have a web page where a member can open up a chat window (child window) with another member. - From there the member can also navigate to other web pages. - From other pages in the site, they...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
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.