By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

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

P: n/a
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[5][5];
d[2][2] = 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,
Adam.

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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[5][5];
d[2][2] = 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

P: n/a
If you change your functions to look like

---- CODE ----

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

int main() {

double d[5][5];
d[2][2] = 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

P: n/a
Adam wrote:
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++
by implementing t1 in C and linking it into your program:
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[0]);
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc t1.o
./main

a[0][0]: 0.000000 a[0][1]: 1.000000 a[0][2]: 2.000000
a[1][0]: 10.000000 a[1][1]: 11.000000 a[1][2]: 12.000000
a[2][0]: 20.000000 a[2][1]: 21.000000 a[2][2]: 22.000000
a[3][0]: 30.000000 a[3][1]: 31.000000 a[3][2]: 32.000000
a[4][0]: 40.000000 a[4][1]: 41.000000 a[4][2]: 42.000000
Jul 23 '05 #4

P: n/a
* James Aguilar:
If you change your functions to look like

---- CODE ----

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

int main() {

double d[5][5];
d[2][2] = 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.

Use e.g. std::vector instead.

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

P: n/a
James Aguilar wrote:

If you change your functions to look like

---- CODE ----

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

int main() {

double d[5][5];
d[2][2] = 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[2][3];

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[1][1] be written as well as a[1][1] and do both expressions
produce the same machine code?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #6

P: n/a
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
just hope the OP didn't follow my advice =/.

- JFA1
Jul 23 '05 #7

P: n/a
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,
Adam.

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.