472,374 Members | 1,377 Online

# passing a 2dimensional array of double to a function...

Greetings all i am having a horrible time trying to pass a 2
dimensional array of doubles to a function... basically a watered down
version of my code looks like:

void t1(double a[][]);

int main() {

double d;
d = 2.3;
t1(d);
}

void t1(double a[][]) {
for(int i=0;i<5;i++)
for (int j=0;j<5;j++)
cout << "a["<<i<<"]["<<j<<"]: "<<a[i][j]<<endl;
}

it keeps giving me all kinds of errors, how exactly do i get a filled 2
dimensional array of doubles to a function? thanks.

Cheers,

Jul 23 '05 #1
7 1644 ff**@hotmail.com wrote:
Greetings all i am having a horrible time trying to pass a 2
dimensional array of doubles to a function... basically a watered down
version of my code looks like:

void t1(double a[][]);

int main() {

double d;
d = 2.3;
t1(d);
}

void t1(double a[][]) {
for(int i=0;i<5;i++)
for (int j=0;j<5;j++)
cout << "a["<<i<<"]["<<j<<"]: "<<a[i][j]<<endl;
}

it keeps giving me all kinds of errors, how exactly do i get a filled 2
dimensional array of doubles to a function? thanks.

You have two options:

const int DIM = 5;
1) void t1(double a[DIM][DIM])
2) void t1(double a[][DIM]) or the equal
void t1(double (*a)[DIM])

--
Regards,

Karsten
Jul 23 '05 #2
If you change your functions to look like

---- CODE ----

void t1(double **a, int y, int x);

int main() {

double d;
d = 2.3;
t1(d, 5, 5);
}

void t1(double **a, int y, int x) {
for(int i=0;i<y;i++)
for (int j=0;j<x;j++)
cout << "a["<<i<<"]["<<j<<"]: "<<a[i][j]<<endl;
}

---- /CODE ----

it would work better.
Jul 23 '05 #3
I am having a horrible time trying to pass a 2-dimensional array of doubles
to a function.
Basically, a watered down version of my code looks like:
Eventually, we expect C++ to adopt C99 style variable size arrays.
Until then, you can work around this deficiency in C++
cat t1.c #include <stdio.h>

void t1(size_t m, size_t n, double a[m][n]) {
for(size_t i = 0; i < m; ++i) {
for (size_t j = 0; j < n; ++j) {
if (0 < j)
fprintf(stdout, "\t");
fprintf(stdout, "a[%u][%u]: %f", i, j, a[i][j]);
}
fprintf(stdout, "\n");
}
}
gcc -Wall -std=c99 -pedantic -c t1.c
cat main.cc #include <cstdlib>

extern "C" {
void t1(size_t m, size_t n, double a[]);
}

int main(int argc, char* argv[]) {
const
size_t m = 5;
const
size_t n = 3;
double d[m][n];
for(size_t i = 0; i < m; ++i)
for (size_t j = 0; j < n; ++j)
d[i][j] = 10.0*i + j;
t1(m, n, d);
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc t1.o
./main

a: 0.000000 a: 1.000000 a: 2.000000
a: 10.000000 a: 11.000000 a: 12.000000
a: 20.000000 a: 21.000000 a: 22.000000
a: 30.000000 a: 31.000000 a: 32.000000
a: 40.000000 a: 41.000000 a: 42.000000
Jul 23 '05 #4
* James Aguilar:
If you change your functions to look like

---- CODE ----

void t1(double **a, int y, int x);

int main() {

double d;
d = 2.3;
t1(d, 5, 5);
}

void t1(double **a, int y, int x) {
for(int i=0;i<y;i++)
for (int j=0;j<x;j++)
cout << "a["<<i<<"]["<<j<<"]: "<<a[i][j]<<endl;
}

The code above is incorrect: an array of arrays is not convertible
to an array of pointers (which would be convertible to a pointer
to an array of pointers, the required function argument).

Don't use raw pointers: for newbies & experienced alike that's an
invitation for disaster.

Off the cuff:

typedef std::vector<double> DoubleVector;
typedef std::vector<DoubleVec> DoubleMatrix;

void display( DoubleMatrix const& m )
{
for( std::size_t row = 0; row <= m.size(); ++row )
{
for( std::size_t col = 0; col < m[row].size(); ++col )
{
double const valueAtRowCol = m[row][col];
}
}
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #5
James Aguilar wrote:

If you change your functions to look like

---- CODE ----

void t1(double **a, int y, int x);

int main() {

double d;
d = 2.3;
t1(d, 5, 5);
}

void t1(double **a, int y, int x) {
for(int i=0;i<y;i++)
for (int j=0;j<x;j++)
cout << "a["<<i<<"]["<<j<<"]: "<<a[i][j]<<endl;
}

---- /CODE ----

it would work better.

Not really :-)

The reason is that the memory layouts of [][] and ** are different.

if you declare
double d;

then 'd' looks in memory like this:

d
+---+---+---+---+---+---+
| | | | | | |
+---+---+---+---+---+---+

| | |
+---- 3 ----+---- 3 ----+

| |
+---- 2 ----+

That's: a piece of memory which gets divided by the compiler into 2 subarrays
of size 3 each. The 2D structure exists only for the compiler and is used for
address calculation of a single element only.

while in t1 you did:

double ** a

which has a memory layout of (again with numbers 2 and 3)

a
+----+ +---+---+---+
| o------->| | | |
+----+ +---+---+---+ +---+---+---+
| o----------------------->| | | |
+----+ +---+---+---+

That is: an array of 2 pointers pointing to arrays of size 3.

As you can see, those structures are completely different.

*But*: The syntax to access a single element is identical in both
cases. Exercise for the reader: Why is this so? Why can
d be written as well as a and do both expressions
produce the same machine code?

--
Karl Heinz Buchegger
Jul 23 '05 #6
Yeah, thank you for correcting me on that. I'm also a relatively young C++
programmer, and I almost never use arrays directly after one bad
experience -- I find vectors much nicer to debug and to write code for. I

- JFA1
Jul 23 '05 #7
thanks for all the help! alomst immediatly after posting the i realized
a solution (isnt that how it always works?) i just ended up using the
STL vector library similarly as someone noted above, but hopfully this
can shed light for everyone.

cheers & thanks,
 4 by: JR | last post by: Hey all, I am passing a two dimensional array to a function. It basically looks like this int test(double array2 , const CONST1, const CONST2) { int returnValue; for (int i = 0;... 1 by: Sam | last post by: Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of... 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. 2 by: Edlueze | last post by: Greetings: I have two functions and I would like to pass the ParamArray gathered from one function to the other function. For the purposes of this post, let's say that they are calculating... 5 by: Michael | last post by: Hi, once I read here that it is not 'a good idea' to pass variables that are not initialized to a function. I have void something ( double *vector ); .... 1 by: PengYu.UT | last post by: Hi, I have the following two functions. However, the function printa gives me a warning. If I delete "const" from its definition, I will not get the warning. I'm wondering if there is anything... 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... 1 by: vijay.gandhi | last post by: Hello, I have created a function in C++/CLI which was exported as a .DLL to be used in VB .NET. I have been having some problems (I think it has to do with the right syntax) with parameter... 4 by: Andreas Reiff | last post by: Hi! I want some communication to take place between a c++ app and a c++ .dll with an intermediate managed/unmanaged c++ dll. Basicall, I want the unmanaged c++ dll to have a callback to the... 0 by: Naresh1 | last post by: What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle... 0 by: antdb | last post by: Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and... 0 by: WisdomUfot | last post by: It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures... 0 by: Oralloy | last post by: Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++... 0 by: Rahul1995seven | last post by: Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts... 1 by: ezappsrUS | last post by: Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the... 0 by: jack2019x | last post by: hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9. 0 by: DizelArs | last post by: Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {... 0 by: F22F35 | last post by: I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...